2017-01-16 10 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

私は_random_ intと_random_ floatを生成する必要があります。しかし、あなたは先生とそれを確認する必要があります。 –

答えて

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

まず、私は文字列にすべての変数を追加し、印刷も、私はそれをしなかった他の方法はhustそのタイプで任意の変数を印刷するには、この権利はありますか?