簡體   English   中英

我如何使我的程序進入python中的下一個elif

[英]How do i make my program move on to the next elif in python

import random
print"hello what is your name?"
name = raw_input()
print"hello", name
print"wanna play a game? y, n"
choice = raw_input()
if choice =='y':
    print'good lets start a number guessing game'

elif choice =='n':
    print'maybe next time'
    exit()

random.randint(1,10)
number = random.randint(1,10)
print'pick a number between 1-10'
numberofguesses = 0
guess = input()

while numberofguesses < 10:
 if guess < number:
    print"too low"
 elif guess > number:
        print"too high"
 elif guess == number:
        print'your correct the number is', number
 break
if guess == number:
    print'CONGRATS YOU WIN THE GAME'

當我在程序中輸入我的猜測時,例如,我只能輸入一個輸出,例如,我輸入8個程序輸出為“太高”,但是當我再次猜測輸出為空白時,如何解決此問題? 你好,你叫什么名字?

 ed
    hello ed
    wanna play a game? y, n
    y
    good lets start a number guessing game
    pick a number between 1-10
    2
    too low
    >>> 5
    5
    >>> 3
    3
    >>> 2
    2
    >>> 

我認為這是您想要的:

numberofguesses = 0

while numberofguesses < 10:
 guess = input()  #int(raw_input("Pick a number between 1 and 10: ")) would be much better here.
 numberofguesses+=1
 if guess < number:
    print "too low"
 elif guess > number:
    print "too high"
 elif guess == number:
    print 'your correct the number is', number
    break

使用您的代碼版本,您只需猜測一次。 如果您錯了,您的程序將一遍又一遍地嘗試相同的猜測(假設您的break實際上應該在elif縮進)。 您可能正在終端中輸入新的猜測,但是您的程序永遠看不到它們。 如果該break實際上在代碼中的正確位置,那么您只需猜測一次,無論是寫入還是錯誤,它都會立即退出循環。

您的休息不在您的ifstatement中

它會在while循環中執行一次,無論發生什么都會中斷

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM