2016-09-14 39 views
-3

這是我的代碼,但正如標題所說,我不能打破,而打破,如果我把東西了,而在外面真和生命去低於0沒有任何反應。Python將不會從真正

from random import randint 
    import sys 
    value = int(randint(1,10)) 
    lives = int(5) 
    userinp = int(0) 
    if lives == 0: 
     print("no lives :(") 
    while True: 
     if lives != 0: 
      print(value) 
      print("You currently have: ", lives, " lives left") 
      userinp = input("Please enter a number from 1 to 10: ") 
      if int(userinp) == int(value): 
       print("Well done you wn :D") 
       sys.exit() 
      elif int(userinp) > int(value): 
       print("Your guess was too high please guess again") 
       lives = lives - 1 
      elif int(userinp) < int(value): 
       print("Your guess was too low please guess again") 
       lives = lives - 1 
    if lives == 0: 
     print("this") 
+4

哪裏*是*你的'break'? – elslooo

+0

'lives = int(5); userinp = int(0)'爲什麼? – DeepSpace

+0

「while True」是一個無限循環,佔用所有CPU週期的100%,並且永遠不會中斷 - 因爲只有當條件變爲False時,纔會結束'',但永遠不會,因爲它始終是' TRUE'! –

回答

0

你必須打破whilelives == 0,或者由break或改變,同時條件。

1

一個while True是一個無限循環,消耗所有的CPU週期的100%,不會破壞 - 因爲當情況變得False一個while <condition>只結束,但絕不可能,因爲它總是True

儘量不要打你循環的性質:

while True: 
    if lives != 0: 
     print(value) 
     print("You currently have: ", lives, " lives left") 
     userinp = input("Please enter a number from 1 to 10: ") 
     if int(userinp) == int(value): 
      print("Well done you wn :D") 
      sys.exit() 
     elif int(userinp) > int(value): 
      print("Your guess was too high please guess again") 
      lives = lives - 1 
     elif int(userinp) < int(value): 
      print("Your guess was too low please guess again") 
      lives = lives - 1 
    if lives == 0: # indent condition to fall within the loop 
     print("this") 
    if lives < 0: # add an extra case to handle what you want 
     break 
2

你的循環本質上是這樣的:

while True: 
    if lives != 0: 
     # do stuff with lives 

這是一個無限循環。即使lives == 0循環不會中斷,因爲此條件作爲循環塊的一部分進行測試,而不是循環條件。 你應該簡單地做:

while lives != 0: 
    # do stuff with lives 

甚至while lives > 0:的情況下,您管理期間1次循環迭代失去了數條生命(似乎不能夠在這裏,但有備無患)。

而且因爲你似乎想學習Python中,有可能要學習,還提高了其他一些東西:

無需整數這裏轉換爲整數:

value = int(randint(1,10)) 
lives = int(5) 
userinp = int(0) 

號如50randint(1,10)本身就是整數。 "5""0"是包含一個數字作爲單個字符的字符串,在與整數進行比較之前需要進行轉換,但這裏不需要。 randint()返回整數。

這是死代碼,因爲你已經設置lives5 2線之上,而不是事後修改了它:

if lives == 0: 
    print("no lives :(") 

您應該檢查用戶是否確實輸入有效的整數轉換值,而不是任意的字符串: (continue會跳到下一個循環迭代,所以它會再次詢問用戶輸入的不遞減lives

userinp = input("Please enter a number from 1 to 10: ") 
if not userinp.isnumeric(): continue 

無需轉換ÿ我們已經一次又一次又一次整數value整數:

if int(userinp) == int(value): 

elif int(userinp) > int(value): 

elif int(userinp) < int(value): 

使用sys.exit()避免的,你可能只是break代替(走出循環,繼續。

最後,Python有得心應手-=, +=, *=, /=運營商,所以你可以寫的lives -= 1代替lives = lives - 1

0

這是我的解決方案,可以專門設置break情況如果發生特定事件時結束while循環。

while lives > 0: 
    print(value) 
    print("You currently have: ", lives, " lives left") 
    userinp = input("Please enter a number from 1 to 10: ") 
    if int(userinp) == int(value): 
     print("Well done you wn :D") 
     sys.exit() 
    elif int(userinp) > int(value): 
     print("Your guess was too high please guess again") 
     lives = lives - 1 
    elif int(userinp) < int(value): 
     print("Your guess was too low please guess again") 
     lives = lives - 1 


print("this")