2017-01-16 5 views
0

Meine Aufgabe:Zuordnung (chatbot)

eine Menüoption erstellen, die das Datum und die Zeit von heute den Bot Druck macht, eine zufällige Stimmung, eine ganze Zahl und ein Schwimmer. Alles sollte in einem String-Satz zusammengefügt werden.

Dies ist, was ich bis jetzt habe, was ich denke, dass ich verwenden sollte. Das Problem ist, ich bin nicht sicher, wie man alles zusammensetzt.

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

Wenn jemand Hilfe oder Tipps zur Verfügung stellen könnte, wie das geht, wäre ich dankbar.

+0

Siehe https://pyformat.info/ –

+2

Wenn Sie Python 3.6+ verwenden, können Sie eine f-Zeichenfolge verwenden: setzen Sie einfach 'f' vor die Anführungszeichen Ihrer Ausgabezeichenfolge:' print (f "" "Today ist {today}. etc "" ")' –

+0

BTW, I _think_ Ihre Aufgabe erfordert, dass Sie ein _random_ int und ein _random_ float generieren. Aber du solltest das mit deinem Lehrer überprüfen. –

Antwort

0

Wenn ich dich verstanden habe, brauchst du so etwas, oder?

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

Zuerst habe ich alle Variablen in String hinzufügen und die Druck es und die andere Art, wie ich es tat hust jede Variable mit seiner Art zu drucken, ist das richtig?