[英]How do I stop a while loop if something happens inside a function that should stop the itterations?
各位程序员,您好,我是 python 的初学者,几个月前。 我决定开始自己的小项目,以帮助我了解 Python 的整个开发过程。 我简要了解了所有基本语法,但我想知道如何在 function 调用 while 循环结束时进行一些操作。
我正在创建一个简单的终端号码猜谜游戏,它由玩家多次尝试猜测 1 到 10 之间的数字(我目前将其设置为 1 以测试代码中的某些内容)。
如果玩家得到正确的数字,则该级别应该结束,然后玩家将进入下一个级别的游戏。 我试图创建一个变量并做出一个真正的错误陈述,但我无法在 while 循环内操作 function 中的变量。
我想知道如何才能使游戏在玩家获得正确的数字时结束,我将在此处包含我的代码,以便你们有更多的上下文:
import random
import numpy
import time
def get_name(time):
name = input("Before we start, what is your name? ")
time.sleep(2)
print("You said your name was: " + name)
# The Variable 'tries' is the indication of how many tries you have left
tries = 1
while tries < 6:
def try_again(get_number, random, time):
# This is to ask the player to try again
answer = (input(" Do you want to try again?"))
time.sleep(2)
if answer == "yes":
print("Alright!, well I am going to guess that you want to play again")
time.sleep(1)
print("You have used up: " + str(tries) + " Of your tries. Remember, when you use 5 tries without getting the correct number, the game ends")
else:
print("Thank you for playing the game, I hope you have better luck next time")
def find_rand_num(get_number, random, time):
num_list = [1,1]
number = random.choice(num_list)
# Asks the player for the number
ques = (input("guess your number, since this is the first level you need to choose a number between 1 and 10 "))
print(ques)
if ques == str(number):
time.sleep(2)
print("Congratulations! You got the number correct!")
try_again(get_number, random, time)
elif input != number:
time.sleep(2)
print("Oops, you got the number wrong")
try_again(get_number, random, time)
def get_number(random, try_again, find_rand_num, time):
# This chooses the number that the player will have to guess
time.sleep(3)
print("The computer is choosing a random number between 1 and 10... beep beep boop")
time.sleep(2)
find_rand_num(get_number, random, time)
if tries < 2:
get_name(time)
tries += 1
get_number(random, try_again, find_rand_num, time)
else:
tries += 1
get_number(random, try_again, find_rand_num, time)
if tries > 5:
break
对于代码中的一些格式,我深表歉意,我尽我所能看起来和我的 IDE 中的一样准确。 我父亲通常会帮助我解决这些类型的问题,但在这一点上,我似乎比我父亲了解更多 python,因为他从事前端 web 开发。 所以,回到我原来的问题,如果这个声明:
if ques == str(number):
time.sleep(2)
print("Congratulations! You got the number correct!")
try_again(get_number, random, time)
是真的,while 循环结束了吗? 另外,我的代码看起来如何? 我花了一些时间使它看起来整洁,从专家的角度来看我很感兴趣。 我曾经在编程中读到过,少即是多,所以我试图用我的代码少花钱多办事。
感谢您花时间阅读本文,如果你们中的某些人对我的问题有任何解决方案,我将不胜感激。 祝你有美好的一天!
您的代码中有太多错误。 首先,您从未使用过在函数中传递的参数,所以我看不出有理由让它们留在那里。 然后你需要从你的函数中返回一些东西来使用它们来打破条件(例如True/False
)。 最后,我想在您的情况下单独调用函数要方便得多,因为您需要在继续之前进行一些检查(不在彼此内部)。 所以,这是我最终得到的代码:
import random
import time
def get_name():
name = input("Before we start, what is your name? ")
time.sleep(2)
print("You said your name was: " + name)
def try_again():
answer = (input("Do you want to try again? "))
time.sleep(2)
# Added return True/False to check whether user wants to play again or not
if answer == "yes":
print("Alright!, well I am going to guess that you want to play again")
time.sleep(1)
print("You have used up: " + str(tries) + " Of your tries. Remember, when you use 5 tries without getting the correct number, the game ends")
return True
else:
print("Thank you for playing the game, I hope you have better luck next time")
return False
# Joined get_number and find_random_number since get_number was doing nothing than calling find_rand_num
def find_rand_num():
time.sleep(3)
print("The computer is choosing a random number between 1 and 10... beep beep boop")
time.sleep(2)
num_list = [1,1]
number = random.choice(num_list)
ques = (input("guess your number, since this is the first level you need to choose a number between 1 and 10 "))
print(ques)
if ques == str(number):
time.sleep(2)
print("Congratulations! You got the number correct!")
# Added return to check if correct answer is found or not
return "Found"
elif input != number:
time.sleep(2)
print("Oops, you got the number wrong")
tries = 1
while tries < 6:
if tries < 2:
get_name()
res = find_rand_num()
if res == "Found":
break
checker = try_again()
if checker is False:
break
# Removed redundant if/break since while will do it itself
tries += 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.