我目前正在從頭開始開發一個聊天機器人,作為我的 Python 課程介紹的作業。在我的作業中,我需要至少有一個“數學組件”。我似乎無法找出如何從字串中減去我的輸入整數。
附件是一個螢屏截圖,我的目標是讓他們輸入每周在家做飯的天數,然后自動從 7 中減去。
print('Hello! What is your name? ')
my_name = input ()
print('Nice to meet you ' my_name)
print('So, ' my_name ' What is your favorite veggie?')
favorite_veggie = input ()
print('Thats nuts! ' favorite_veggie ' is mine too!')
print('How many days a week do you have cook at home? ')
day = input ()
print('So what do you do the other ' ????? 'days?')
uj5u.com熱心網友回復:
你正在尋找這個
day = input()
required_days = 7 - int(day)
print('So what do you do the other ' str(required_days) ' days?')
uj5u.com熱心網友回復:
day = int(input())
print('So what do you do the other', 7-day, 'days?')
uj5u.com熱心網友回復:
您可以將 的結果轉換input為int,然后進行減法。也input("")接受一個字串作為引數,它會顯示在提示區的前面,這使得代碼更好
my_name = input('Hello! What is your name? ')
print('Nice to meet you ' my_name)
favorite_veggie = input('So, ' my_name ' What is your favorite veggie?')
print('Thats nuts! ' favorite_veggie ' is mine too!')
day = int(input('How many days a week do you have cook at home? '))
non_work_day = 7 - day
what_do = input('So what do you do the other ' str(non_work_day) 'days?')
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/351731.html
