如何清除变量/定义

时间:2018-10-31 18:39:50

标签: python

我在Enthought Canopy Free Version上有一些代码。在这里,我试图使其不重复秘密功能。

$request->Desciption

重复from __future__ import print_function import random def guess_once(): secret = random.randint(1,10) print('i have a number between 1 and 10') guess = int(raw_input('guess: ')) if guess != secret: if guess not in (1,2,3,4,5,6,7,8,9,10): if guess < 1: print ("Nope, too low") guess_once() elif guess > 10: print ("Nope, too high") guess_once() print('wrong, my number is ', secret, '.', sep='') else: print('aight, ya got it boi, my number was',guess, nd='!/n') #this is just to start it like a lazy person guess_once() 行是因为"Wrong my secret was #"函数每次重定向回到起点时都会运行,该如何解决?

1 个答案:

答案 0 :(得分:0)

创建多个功能,这样您就不必重复自己了

伪代码:

def get_user_guess():
   val = int(input("guess (1-10)")
   while val < 1 or val > 10:
      print("bad val")
      val = int(input("guess (1-10)")
   return val

def play_game():
   secret = randint()
   guessed_val = get_user_guess()
   if guess == secret:
     print("smarty pants")
   else:
     print("nope")

play_game()