2013-10-28 109 views
-1

我正在做一個任務,我必須爲不同的主題進行測驗。這是我的代碼到目前爲止。Python編程循環

print("Hello and welcome to Shahaad's quiz!") #Introduction 
name = input("What is your name? ") 
print("Alright", name,", these will be today's topics:") 
print("a) Video Games") 
print("b) Soccer") 
print("c) Geography") 
choice = input("Which topic would you like to begin with?") 
if choice == 'video games' or choice == 'Video Games' or choice == 'Video games' or choice == 'a)': 
    print("You picked Video Games.") 
print("Question number one:") 
print("What is the most popular FPS (First Person Shooter) game?") 
print("a) Call of Duty") 
print("b) Battlefield") 
print("c) Grand Theft Auto 5") 
print("d) Counter Strike") 
answer = input("Your answer:") 
guessesTaken = 0 
if answer == 'Call Of Duty' or answer == 'Call of duty' or answer == 'Call of duty' or answer == 'a)' or answer == 'call of duty': 
    print("You are correct!") 
else: 
    guessesTaken = guessesTaken + 1 
    print("Incorrect!") 
    print("You have", guessesTaken, "guess left!") 

我試圖讓這個,如果他們得到的答案錯了,他們得到的回答這個問題的另一個機會。現在一旦他們弄錯了,他們不能再打字了。謝謝!

+5

。提示:'while'。 – Kevin

回答

1

您應該這樣做@BartoszKP說,使用while循環來檢查用戶是否輸入了有效的輸入。

這就是說,我有一些建議可能會提高您的代碼的可讀性。 取而代之的是

if choice == 'video games' or choice == 'Video Games' or choice == 'Video games' or choice == 'a)': 
    print("You picked Video Games.") 

你可以採取str().lower()方法的優點:

if choice.lower() == 'video games' or choice == 'a': 
    print('You picked Video Games.") 

下()方法將所有字母爲小寫。

關於while循環,我不喜歡使用標誌變量 - 它向代碼添加了一個額外的變量,它並不是真正需要的。相反,你可以利用break

while True: 
    choice = input('Which topic would you like to begin with?') 
    if choice.lower() == 'video games' or 'a': 
     print('You picked Video Games.') 
     break #This breaks out of the while loop, and continues executing the code that follows the loop 

另一種解決方案是while循環之前定義choice變量,並運行它,直到輸入是像你想:

choice = input('Which topic would you like to begin with?') 
while choice.lower() != 'video games' and choice != 'a': 
    print('Please pick a valid option') 
    choice = input('Which topic would you like to begin with?') 
print('You picked "{}".'.format(choice)) 

如果你想能夠選擇不同的選項,通過檢查輸入字符串是否是列表中的項目之一,可以進一步改進代碼:

valid_options = ['video games', 'a', 'software', 'b', 'cartoons', 'c'] 
choice = input('Which topic would you like to begin with?') 
while choice.lower() not in valid_options: 
    print('Please pick a valid option') 
    choice = input('Which topic would you like to begin with?') 
print('You picked "{}".'.format(choice)) 

輸出:

Which topic would you like to begin with?Movies 
Please pick a valid option 
Which topic would you like to begin with?vIdeO gaMes 
You picked "vIdeO gaMes". 

Which topic would you like to begin with?software 
You picked "software". 

如果您使用Python 2.x中,你也應該考慮使用raw_input()代替input()。請參閱this相關的SO問題來理解爲什麼。

1

這是一個簡單且經常遇到的問題。該解決方案通常符合這樣的場景:

flag = False 

while not flag: 
    x = input("Get input from the user:") 

    if validate(x): 
     flag = True 
    else: 
     print "Input invalid. Try again" 

當變量名應該是當然的改變爲適合於當前的任務(例如flag - >answerCorrect或類似,x - >answer等)。

+0

在我的情況下,我需要在哪裏插入?我現在有點困惑。 –

+1

@ShahaadBoss只需停下一會兒,試着慢慢思考。循環重複行爲。你的情況需要重複什麼?所有需要重複的內容都應該在一個循環內。您需要多次提問,並多次驗證。所以,至少提出問題並驗證它應該是循環的。 之後,請務必仔細檢查是否有可能完成循環(在這種情況下:「flag」是否有機會設置爲「True」,以便循環不會無限)。爲了簡單起見,考慮將你的代碼分割成函數。 – BartoszKP