【问题标题】:Assignment (chatbot)作业(聊天机器人)
【发布时间】:2017-01-16 11:41:54
【问题描述】:

我的任务:

创建一个菜单选项,让机器人打印今天的日期和时间、随机情绪、整数和浮点数。一切都应该放在一个字符串句子中。

这是我到目前为止所得到的,我认为我应该使用它。问题是我不确定如何将它们组合在一起。

def dateandfeels():
    """
    docstring
    """
    today = (time.strftime("%H:%M:%S"))
    time1 = (time.strftime("%d/%m/%Y"))
    whole = int(15)
    float1 = float(0.056)
    mood = ['happy', 'sad', 'moody', 'pythonic']
    moody = (random.choice(mood))

    print("""Today is {today}. Time is {time1}. My number of choice is {whole}
    and my float of choice is {float1}. Also I'm feeling a bit {moody} today""")

如果有人能提供一些帮助或提示,我将不胜感激。

【问题讨论】:

  • 如果您使用的是 Python 3.6+,您可以使用 f 字符串:只需将 f 放在输出字符串的引号之前:print(f"""Today is {today}. etc""")
  • 顺便说一句,我认为你的任务要求你生成一个 random int 和一个 random float。但是你应该和你的老师核实一下。

标签: python string random


【解决方案1】:

如果我理解你,你需要这样的东西,对吧?

import time
import random
def dateandfeels():
  """
  docstring
  """
  toPrint="";
  today = (time.strftime("%H:%M:%S"))
  toPrint+=today;
  time1 = (time.strftime("%d/%m/%Y"))
  toPrint+=" "+time1;
  whole = int(15)
  toPrint+= " "+str(whole)
  float1 = float(0.056)
  toPrint+=" "+str(float1);
  mood = ['happy', 'sad', 'moody', 'pythonic']
  toPrint+=" "+''.join(mood);
  moody = str((random.choice(mood)))
  toPrint+=" "+moody;
  print("Here print the string: \n")
  print (toPrint+"\n")
  print("Here use printf like to print all variables: \n")
  print ("Today is %s. Time is %s. My number of choice is %d and my float of choice is %f. Also I'm feeling a bit %s today" %(today,time1,whole,float1,moody))

dateandfeels()

首先我将所有变量添加到字符串中并打印它,另一种方法是打印任何具有其类型的变量,对吗?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-12-23
    • 1970-01-01
    • 1970-01-01
    • 2018-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多