2017-01-16 124 views
0

我的任务:分配(聊天机器人)

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

这是我到目前为止我认为应该使用的。问题是我不确定如何把它放在一起。

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

如果任何人可以提供一些帮助或提示如何得到这个进展,我将不胜感激。

+0

查看https://pyformat.info/ –

+2

如果您使用的是Python 3.6或更高版本,则可以使用f-字符串:只需在输出字符串的引号之前加上'f:'print(f“”“Today是{}今天。等“”“)' –

+0

BTW,我_think_你的任务需要你生成一个_random_ int和一个_random_浮动。但你应该和你的老师核对一下。 –

回答

0

如果我理解你,你需要这样的事情,对不对?

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

首先我添加的所有变量转换为字符串,并打印它和其他的方式,我这样做是华中科技大学与它的类型打印任何变量,这是正确的?