2017-06-06 33 views
0

我正在製作一個項目,並想知道如何使用代碼來判斷用戶是否輸入了「男性」,「女性」,「男性」或「女性」之外的內容。如何判斷用戶是否輸入了男性,男性,女性或女性以外的其他內容?

我使用Python 3.0

這是我的代碼:

import time 
def w(t): 
    if t == t: 
    time.sleep(t) 
print("In the following story, you are a mouse. Please fill out the following information (the story will use this name and gender).") 
w(3) 
print('\n') 
name = input("Your Name:") 
w(0.5) 
print('\n') 
gender = input("Your Gender (Male/Female):") 
w(0.5) 
print('\n') 
input("Press Enter to Begin The Story") 
w(1) 
print('\n') 
print("The Lion and The Mouse") 
print("A Short Tale by Aesop") 
w(1) 
print('\n') 
print("Once upon a time in a secluded jungle, there was a mouse.") 
w(2) 
print('\n') 
if gender == ("male" or "Male"): 
    heorshe = "he" 
    hisher = "his" 
    himher = "him" 
    himselfherself = "himself" 
    HeShe = "He" 
if gender == "female" or "Female": 
    heorshe = "she" 
    hisher = "her" 
    himher = "her" 
    himselfherself = "herself" 
    HeShe = "He" 

我也想能夠告訴我們,如果用戶在什麼到輸入已進入!請幫忙!

+0

'性別==(「男性」或「男性」):'是不是你怎麼查。你需要將性別與個人進行比較。一個更好的計劃,首先是小寫輸入,然後只檢查「男性」。 – Carcigenicate

+0

另外,'if t == t:'應該完成什麼?我只知道一個會失敗的單一值。 – Carcigenicate

+0

@Carcigenicate謝謝!我會嘗試將答案轉換爲小寫,看看它是否正確運行!但如果你知道如何判斷輸入是空的還是數值的答案,請告訴我! –

回答

0

你可以只使用else:,或if not 'male' or not 'Male:'然後打印例如'Please enter Male or Female.'

+0

這不起作用,當我測試它。當我運行輸入並按下回車鍵而沒有輸入任何內容時,它沒有運行代碼:print(「請輸入男性或女性」)。請詳細說明! –

+0

我的休息時間,我必須離開,但我會在美國東部時間下午3點左右回來檢查您的回覆或答案。 –

+0

問題在於它運行的是打印語句,但是在您檢查用戶輸入的內容之後,因爲這是故事剛剛開始的代碼末尾。但這[post]的第一個答案(https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response)也適用於你。 – MushroomMauLa

相關問題