为啥 gcode == code 或 code1 或 code2 或 code3 不能在任何地方工作?
Posted
技术标签:
【中文标题】为啥 gcode == code 或 code1 或 code2 或 code3 不能在任何地方工作?【英文标题】:Why wouldn't gcode == code or code1 or code2 or code3 work anywhere?为什么 gcode == code 或 code1 或 code2 或 code3 不能在任何地方工作? 【发布时间】:2022-01-16 12:02:56 【问题描述】:这是计算机科学课程的作业。有没有办法让它工作,或者更有效地做到这一点?该程序的目标是让用户猜测所有四个数字(不必按顺序),告诉他们它们是否正确并显示尝试次数。该赋值要求至少包含一个带参数的函数和一个列表。
import random
# Sets tries and isCorrect to 0- not broken
tries = 0
isCorrect = 0
# Generates 4 random numbers- not broken
for i in range(4):
code = random.randint(1, 9)
code1 = random.randint(1, 9)
code2 = random.randint(1, 9)
code3 = random.randint(1, 9)
# Prints the random numbers for now- not broken
print(code)
print(code1)
print(code2)
print(code3)
# Guess = If the guess variables are equal to the code it tells you you're right
def guess(a, b, c, d):
global isCorrect
if a == code or code1 or code2 or code3 and b == code or code1 or code2 or code3 and c == code or code1 or code2 or code3 and d == code or code1 or code2 or code3:
print("You got it correct!")
else:
print("You got it wrong.")
return(a, b, c, d)
# While isCorrect is still 0 it prompts you to guess again, if it's correct it sets isCorrect to one
while isCorrect == 0:
gcode = int(input("Input your first guess: "))
gcode1 = int(input("Input your second guess: "))
gcode2 = int(input("Input your third guess: "))
gcode3 = int(input("Input your fourth guess: "))
guess(gcode, gcode1, gcode2, gcode3)
tries = tries + 1
#print("You got it wrong.")
if gcode == code or code1 or code2 or code3 and gcode1 == code or code1 or code2 or code3 and gcode2 == code or code1 or code2 or code3 and gcode3 == code or code1 or code2 or code3:
isCorrect = 1
# Makes it so the plural matches amount of tries
if tries != 1:
print("It took you", tries, "tries.")
else:
print("It took you", tries, "try.")
【问题讨论】:
投反对票,因为标题与代码不匹配。确保标题是所提出的实际问题的准确摘要。 (标题中显示的具体问题 --x == a or b or c
-- 是一个常见的 Python 错误,但实际上并没有出现在代码中..)
如果我的问题写得不好,谁能解释一下原因?
标题:.. gcode == code or code1 or code2 or code3 ..
-- 这如何代表代码?
我已经编辑了帖子以更好地匹配标题。
这能回答你的问题吗? How to test multiple variables against a single value?
【参考方案1】:
我认为你必须马上给出你的任务,因此我的帮助 :-) 我还添加了列表的使用。
这是工作代码,但请仔细检查。
import random
# Sets tries and isCorrect to 0- not broken
tries = 0
isFalse = True # use boolean variable to make it more clear - 0 would be an integer
# Create a list in which you store the 4 random values
# Initialize the empty list, so it is defined in the for loop
random_values_list = []
# Generates 4 random numbers- not broken
for i in range(4):
# append 4 times a random value to the list
random_values_list.append(random.randint(1, 9))
# sort the list here is important, later we check the sorted input against this list
random_values_list.sort()
# Prints the random numbers
print(random_values_list)
# Guess = If the guess variables are equal to the code it tells you you're right
def check_guess(input_guesses_list, random_values_list):
print(input_guesses_list)
print(random_values_list)
# now you can simply check if the lists are the same
if input_guesses_list == random_values_list:
print("You got it correct!")
# return false to break the while loop, because the user got it right
return False
else:
print("You got it wrong.")
# return true to keep the while loop alive
return True
# Save the inputs also in a list
input_guesses_list = []
# While isCorrect is still 0 it prompts you to guess again, if it's correct it sets isCorrect to one
while isFalse:
input_guesses_list.append(int(input("Input your first guess: ")))
input_guesses_list.append(int(input("Input your second guess: ")))
input_guesses_list.append(int(input("Input your third guess: ")))
input_guesses_list.append(int(input("Input your fourth guess: ")))
# also sort this list
input_guesses_list.sort()
isFalse = check_guess(input_guesses_list, random_values_list)
tries += 1
# important: reset the list with the guesses!
input_guesses_list = []
# Makes it so the plural matches amount of tries
if tries != 1:
print("It took you", tries, "tries.")
else:
print("It took you", tries, "try.")
【讨论】:
这工作正常。谢谢。我会仔细阅读代码并尝试理解它,这样我以后就不会出错了。【参考方案2】:你的 if 语句是错误的。这不是在 python 中使用or
的方式。
这是正确的方法:
if gcode == code or gcode == code1 or gcode == code2 or gcode == code3 and ...
按照您的编写方式,它将 code1(或 code2、3、..)作为表达式并检查它们是否为真。这不是你想要的。
当然,这是一种混乱的编码方式。一种更好的方法是将代码放在一个列表中,将 gcodes 放在另一个列表中,然后在 for 循环中检查它们。
【讨论】:
当有 3 个或更多变量时,if gcode in [code, code1, code2, code3, ...]
更易于阅读,并且可能更 Pythonic。以上是关于为啥 gcode == code 或 code1 或 code2 或 code3 不能在任何地方工作?的主要内容,如果未能解决你的问题,请参考以下文章
npm 错误!代码 1.为啥这个错误消息会发生 npm ERR!代码1?我该如何解决