我的任务是:
创建一个菜单选项,让机器人打印今天的日期和时间、随机情绪、整数和浮点数。所有的东西都应该放在一个字符串句子里。
到目前为止,这就是我认为应该使用的东西。问题是我不知道该怎么把这一切组合起来。
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""")如果有人能提供一些帮助或建议如何使这件事进行,我将不胜感激。
发布于 2017-01-16 12:08:31
如果我明白你的意思,你需要这样的东西,对吧?
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()首先,我将所有变量添加到字符串中,然后打印它,而另一种方法是打印任何具有其类型的变量,对吗?
https://stackoverflow.com/questions/41675620
复制相似问题