Python 猜谜游戏不工作
Python guessing game not working
我正在制作一个 python 代码,它会选择一个随机数并将其与用户的猜测进行比较。
import random
attempts=0
secret=random.randint(1,49)
print "welcome to my guessing game"
repeat
def repeat():
guess=raw_input("I have thought of a number between 1 and 50. you have to try and guess it")
if secret==guess:
print "Well Done! you guessed it in "+attempts+" attempts"
elif secret < guess:
print "too high"
guess=raw_input("have another go")
elif secret > guess:
print "too low"
guess=raw_input("have another go")
attempts += 1
while guess != secret and attempts>6:
repeat()
但是说repeat没有定义
我已将您的代码修改为以下应该有效的代码。您可能需要阅读 Local and Global Variables,因为这是导致您的代码出现主要问题的原因。
import random
def repeat(secret, attempts):
guess=raw_input("I have thought of a number between 1 and 50. you have to try and guess it")
if secret==guess:
print "Well Done! you guessed it in "+attempts+" attempts"
elif secret < guess:
print "too high"
guess=raw_input("have another go")
elif secret > guess:
print "too low"
guess=raw_input("have another go")
attempts += 1
while guess != secret and attempts < 6:
repeat(secret, attempts)
attempts = 0
secret = random.randint(1,49)
print "welcome to my guessing game"
repeat(secret, attempts)
这将允许用户猜测 7 次,然后打印游戏结束:
这是为了 Python 2.为了 Python 3.使用 int(input(""))
import random
secret = random.randint(1,49)
attempts = 0
for attempts in range(7):
guess=input("I have thought of a number between 1 and 50. you have to try and guess it: ")
if secret==guess:
print "Well Done! you guessed it "
elif secret < guess:
print "too high"
guess=input(" Enter to have another go")
elif secret > guess:
print "too low"
guess=input("Enter to have another go")
if attempts == 6:
print "Game Over"
该程序无法运行,因为您无法 运行 或在创建之前调用函数。你也应该通过这样做来调用 repeat "repeat()"
我正在制作一个 python 代码,它会选择一个随机数并将其与用户的猜测进行比较。
import random
attempts=0
secret=random.randint(1,49)
print "welcome to my guessing game"
repeat
def repeat():
guess=raw_input("I have thought of a number between 1 and 50. you have to try and guess it")
if secret==guess:
print "Well Done! you guessed it in "+attempts+" attempts"
elif secret < guess:
print "too high"
guess=raw_input("have another go")
elif secret > guess:
print "too low"
guess=raw_input("have another go")
attempts += 1
while guess != secret and attempts>6:
repeat()
但是说repeat没有定义
我已将您的代码修改为以下应该有效的代码。您可能需要阅读 Local and Global Variables,因为这是导致您的代码出现主要问题的原因。
import random
def repeat(secret, attempts):
guess=raw_input("I have thought of a number between 1 and 50. you have to try and guess it")
if secret==guess:
print "Well Done! you guessed it in "+attempts+" attempts"
elif secret < guess:
print "too high"
guess=raw_input("have another go")
elif secret > guess:
print "too low"
guess=raw_input("have another go")
attempts += 1
while guess != secret and attempts < 6:
repeat(secret, attempts)
attempts = 0
secret = random.randint(1,49)
print "welcome to my guessing game"
repeat(secret, attempts)
这将允许用户猜测 7 次,然后打印游戏结束:
这是为了 Python 2.为了 Python 3.使用 int(input(""))
import random
secret = random.randint(1,49)
attempts = 0
for attempts in range(7):
guess=input("I have thought of a number between 1 and 50. you have to try and guess it: ")
if secret==guess:
print "Well Done! you guessed it "
elif secret < guess:
print "too high"
guess=input(" Enter to have another go")
elif secret > guess:
print "too low"
guess=input("Enter to have another go")
if attempts == 6:
print "Game Over"
该程序无法运行,因为您无法 运行 或在创建之前调用函数。你也应该通过这样做来调用 repeat "repeat()"