我认为说我几天前开始编码很重要。
因此,我制作了这款Rock,Paper,Scissors游戏,可让您与电脑对战(实际上是按照udemy训练营课程进行的)。
这一切都很好但现在由于某种原因,翻译不会移动到下一个代码块 因此,它将处理机器选择部分,但不会继续进行赢家计算。
我不知道出了什么问题,因为我没有改变代码。 我在代码中留下了评论,以便您更好地了解我
感谢您的帮助!
import random
print ("ROCK PAPER SCISSORS")
Human_choice = input("Human, what's your choice?").lower()
hw = "The human won!"
mw = "The machine won!"
tie = "It's a tie"
#start of machine choice
machince_choice = random.randint(1,3)
if machince_choice == 1:
machince_choice = "Rock"
elif machince_choice == 2:
machince_choice = "Paper"
elif machince_choice == 3:
machince_choice = "Scissors"
print(f"Machine plays {machince_choice}")
#end of machine choice
#it seems like from here on it just won't process
if Human_choice == machince_choice:
print(tie)
if Human_choice == "Rock":
if machince_choice == "Paper":
print (mw)
elif machince_choice =="Scissors":
print (hw)
if Human_choice == "Paper":
if machince_choice == "Rock":
print (hw)
elif machince_choice =="Scissors":
print (mw)
if Human_choice == "Scissors":
if machince_choice == "Rock":
print (mw)
elif machince_choice =="Paper":
print (hw)
if machince_choice == "Rock":
if Human_choice == "Paper":
print (hw)
elif Human_choice =="Scissors":
print (mw)
if machince_choice == "Paper":
if Human_choice == "Rock":
print (mw)
elif Human_choice =="Scissors":
print (hw)
if machince_choice == "Scissors":
if Human_choice == "Rock":
print (hw)
elif Human_choice =="Paper":
print (mw)
答案 0 :(得分:1)
当评论回应时,这些陈述将始终返回false:
if Human_choice == machince_choice:
if Human_choice == "Rock":
if Human_choice == "Paper":
etc...
为什么呢? 看看这一行:
Human_choice = input("Human, what's your choice?").lower()
您将.lower()
应用于输入字符串,并且您正在使用大写元素与字符串进行比较。
解决方案:
将machine_choice
和您测试等效的所有其他字符串设置为小写,或者执行以下操作:
Human_choice = input("Human, what's your choice?")
Human_choice = Human_choice[0].upper() + Human_choice[1:].lower()
现在你有一个字符串,其中第一个字母为高位,其余字母为低位;就像你的等价陈述一样。不过这种情况很糟糕,我建议不要这样做。