抱歉,标题令人困惑,我真的想不出该如何表达。 本质上,我有下面的代码。
由于某种原因,“后退”变量将不会成为随机变量。没有错误消息,所以我不知道我在做什么错。谢谢!
加分点:如何在循环中设置此值,使其自动将以前的值设置为“起始值”?
#import random
import random
randomCard = ("X", "O")
front = ""
back = ""
rand = random.choice(randomCard)
#ask what's on front
starterFront = input("What's on front? (Starter)")
starterBack = input("What was on back?")
print("")
#start
front = input("What's on front?")
if front == "X" and starterFront == "X" and starterBack == "X":
back = "O"
print(back)
if front == "X" and starterFront == "X" and starterBack == "O":
back = (random.choice(randomCard))
print(back)
if front == "O" and starterFront == "O" and starterBack == "X":
back = (random.choice(randomCard))
print(back)
if front == "X" and starterFront == "O" and starterBack == "X":
back = (random.choice(randomCard))
print(back)
if front == "O" and starterFront == "X" and starterBack == "O":
back = (random.choice(randomCard))
print(back)
答案 0 :(得分:0)
您的后退变量正在调用随机选择back = (random.choice(randomCard))
,因此它将与随机变量不同。要使其相同,只需使用back = rand
,它将分配回rand的值。
要使其陷入循环,请使用while 'input':
循环。
先前的值也应保持不变,因此您无需对其进行任何操作。
#import random
import random
randomCard = ("X", "O")
front = ""
back = ""
rand = random.choice(randomCard)
#ask what's on front
starterFront = input("What's on front? (Starter)")
starterBack = input("What was on back?")
print("")
#start
front = input("What's on front?")
while front:
if front == "X" and starterFront == "X" and starterBack == "X":
back = "O"
print(back)
if front == "X" and starterFront == "X" and starterBack == "O":
back = rand
print(back)
if front == "O" and starterFront == "O" and starterBack == "X":
back = rand
print(back)
if front == "X" and starterFront == "O" and starterBack == "X":
back = rand
print(back)
if front == "O" and starterFront == "X" and starterBack == "O":
back = rand
print(back)
front = input("What's on front?")