作业(聊天机器人)

2024-03-28 16:45:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我的任务:

Create a menu option which makes the bot print the date and time of today, a random mood, an integer and a float. Everything should be put together in a string sentence.

这是我到目前为止得到的,我认为我应该用的。问题是我不知道怎么把它们放在一起。你知道吗

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""")

如果有人能提供一些帮助或提示如何让这个进行我将不胜感激。你知道吗


Tags: andofthetodaytimeisrandomfloat
1条回答
网友
1楼 · 发布于 2024-03-28 16:45:38

如果我理解你,你就需要这样的东西,对吗?你知道吗

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()

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

相关问题 更多 >