2016-09-14 3 views
-3

これは私のコードですが、タイトルは私が壊れていないと言っています。Pythonはしばらく時間を掛けません。True

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

*は*あなたの '休憩 'ですか? – elslooo

+0

'lives = int(5); userinp = int(0) 'なぜですか? – DeepSpace

+0

'while True'は無限ループで、すべてのCPUサイクルの100%を消費し、決して中断しません。なぜなら' while 'は条件が' False'になると終了しますが、本当! –

答えて

0

あなたはbreakまたはwhile条件を変更することにより、いずれか、whileときlives == 0を破る必要があります。

1

while Trueは、すべてのCPUサイクルの100%を消費し、無限ループで、壊れることはありません - それは常にTrueあるので、条件がFalseになったときwhile <condition>だけで終わるが、それはなることはありませんので、!これは無限ループです

while True: 
    if lives != 0: 
     # do stuff with 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: # indent condition to fall within the loop 
     print("this") 
    if lives < 0: # add an extra case to handle what you want 
     break 
2

はあなたのループの性質を戦っていない試してみてください。 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) 

数値は、 5および0、およびrandint(1,10)はそれ自身で整数です。 "5""0"は、数字を1文字として含む文字列であり、整数と比較する前に変換する必要がありますが、ここでは必要ありません。 randint()は整数を返します。ユーザが実際に代わりに任意の文字列の有効な整数変換可能な値を入力した場合

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

は、あなたがチェックする必要があります:

これは上記のにラインを設定していない後でそれを変更したため、デッドコードは次のようになります。それはlives

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

をデクリメントせずに再入力のためのyを変換する必要をユーザーに尋ねるませんので、(continueは、次のループの繰り返しにスキップします私たち、すでに何度も何度も何度も整数にvalueを整数:

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

elif int(userinp) > int(value): 

elif int(userinp) < int(value): 

避けsys.exit()を使用して、あなただけのbreak代わりに(ループから抜け出すと、継続することがあります。

そして、あなたはlives -= 1を書くことができる代わりに、これは私のソリューションですlives = lives - 1

0

、あなたは特定のイベントが発生した場合、あなたのwhileループを終了するために、具体的break条件を設定することができますので、最後に、Pythonは、便利な-=, +=, *=, /=の演算子を持っています。

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") 
関連する問題