Python 3.x 为什么这段代码没有按照我想要的方式进行处理?
当我运行这段代码时,程序不会按我想要的方式运行。 当我回答第二个问题时,程序执行相同的程序,就像我以错误的方式回答第一个问题一样。Python 3.x 为什么这段代码没有按照我想要的方式进行处理?,python-3.x,Python 3.x,当我运行这段代码时,程序不会按我想要的方式运行。 当我回答第二个问题时,程序执行相同的程序,就像我以错误的方式回答第一个问题一样。 有人能看出我的错误在哪里吗 my_money = 100 first_question = input("Who do you bet for? ") if first_question == "tim" or first_question == "tess" or first_question == "alex" or first_question == "du
有人能看出我的错误在哪里吗
my_money = 100
first_question = input("Who do you bet for? ")
if first_question == "tim" or first_question == "tess" or first_question == "alex" or first_question == "duck" or first_question == "dog":
bet = int(input("How much you bet? "))
if bet < 0:
print("The bet can't take negative valours! ")
bet = int(input("How much you bet? "))
while bet < 0:
print("The bet can't take negative valours! ")
bet = int(input("How much you bet? "))
if bet > my_money:
print("The bet can't be above the money you have! ")
bet = int(input("How much you bet? "))
while bet > my_money:
print("The bet can't be above the money you have! ")
bet = int(input("How much you bet? "))
if bet >= 0 and bet <= my_money:
total_money = my_money - bet
if first_question != "tim" or first_question != "tess" or first_question != "alex" or first_question != "duck" or first_question != "dog":
print("There is no runner with that name. ")
first_question = input("Who do you bet for?")
while first_question != "tim" or "tess" or "alex" or "duck" or "dog":
print("There is no runner with that name. ")
first_question = input("Who do you bet for? ")
我的钱=100
第一个问题=输入(“你为谁下注?”)
如果第一个问题==“tim”或第一个问题==“tess”或第一个问题==“alex”或第一个问题==“duck”或第一个问题==“dog”:
bet=int(输入(“你赌了多少?”)
如果下注<0:
打印(“赌注不能接受负英勇!”)
bet=int(输入(“你赌了多少?”)
下注<0时:
打印(“赌注不能接受负英勇!”)
bet=int(输入(“你赌了多少?”)
如果打赌>我的钱:
打印(“赌注不能超过你的钱!”)
bet=int(输入(“你赌了多少?”)
而打赌>我的钱:
打印(“赌注不能超过你的钱!”)
bet=int(输入(“你赌了多少?”)
如果bet>=0并且bet这里有一个更好的代码版本,我认为我可以为您工作
myMoney = 100
runnerName = input("Who do you bet for? ")
runners = ["tim", "tess", "alex", "duck", "dog"]
while runnerName not in runners:
print("There is no runner with that name. ")
runnerName = input("Who do you bet for? ")
bet = int(input("How much you bet? "))
while bet < 0 and bet > myMoney:
if bet < 0:
print("The bet can't take negative valours! ")
bet = int(input("How much you bet? "))
else:
print("The bet can't be above the money you have! ")
bet = int(input("How much you bet? "))
moneyLeft = myMoney - bet #The program will reach here if the value of bet is between 0 and myMoney no need of if.
print(moneyLeft)
myMoney=100
runnerName=input(“您为谁下注?”)
跑步者=[“蒂姆”、“苔丝”、“亚历克斯”、“鸭子”、“狗”]
当runnerName不在Runner中时:
打印(“没有具有该名称的跑步者。”)
runnerName=input(“您为谁下注?”)
bet=int(输入(“你赌了多少?”)
当bet<0和bet>myMoney时:
如果下注<0:
打印(“赌注不能接受负英勇!”)
bet=int(输入(“你赌了多少?”)
其他:
打印(“赌注不能超过你的钱!”)
bet=int(输入(“你赌了多少?”)
moneyLeft=myMoney-bet#如果下注值介于0和myMoney之间,则程序将到达此处,无需if。
打印(moneyLeft)
为了清楚起见,您可以在运行程序时发布输入和程序的输出吗?在接近结尾时,您可以在第一个问题时读取一行内容“蒂姆”或“苔丝”或“亚历克斯”或“鸭子”或“狗”:
。这不是在做你认为的事情。它不是检查first\u question
是否不是这些值中的任何一个,而是只检查first\u question!='tim'
,然后检查每个字符串的强制布尔值。非空字符串将返回True
(自己尝试bool(“duck”)
)。所以最后,你的while语句实际上是:while(first_-question!=“tim”)或True或True或True或True:
这当然会计算为while
,而不管first_-question
是什么。但是我的第一个代码有什么问题?@LittleBlue while循环最终在接受输入后什么也不做。它只接受输入,程序退出。尽量减少代码的冗余。这将帮助您更好地找到bug。我认为你是一个初学者,你会慢慢学习的。社区随时都会提供帮助。:)是的,我是一个初学者。还有一件事,我如何设置海龟能走的最大距离?