practicepython.org上的Python 3奶牛和猪游戏
游戏是生成4个随机数。用户需要猜测一个数字。如果猜错了,用户就会得到一头猪。如果猜测正确,用户将得到一头牛。 我知道有解决办法。我试图理解我的代码有什么问题。 电路板无法打印,我一直在分号第15行出错practicepython.org上的Python 3奶牛和猪游戏,python,Python,游戏是生成4个随机数。用户需要猜测一个数字。如果猜错了,用户就会得到一头猪。如果猜测正确,用户将得到一头牛。 我知道有解决办法。我试图理解我的代码有什么问题。 电路板无法打印,我一直在分号第15行出错 import random s = [] s[0] = randint(0,9) s[1]= randint(0,9) s[2] = randint(0,9) s[3]= randint(0,9) board = ('s[0] + ' | '
import random
s = []
s[0] = randint(0,9)
s[1]= randint(0,9)
s[2] = randint(0,9)
s[3]= randint(0,9)
board = ('s[0] + ' | '+ s[1] +' | '+ s[2] +' | '+ s[3]')
# user guess a number
u1 = input('Your Name')
pig= 0
cow = 0
match = 0
while match != 4:
uu = int(input("Enter a number 0-9")#user input
if uu == s[0]:
s[0].replace(uu)
cow +=1
elif uu == s[1]:
s[1].replace(uu)
cow+=1
elif uu == s[2]:
s[2].replace(uu)
cow +=1
elif uu == s[3]:
s[3].replace(uu)
cow += 1
else:
print('Wrong')
pig = 0
continue
print (u1+ ' Your cows ' + cow " and pigs "+ pig)
- 对列表的索引赋值要求列表在该索引处保存一个值李>
- 无法打印
int+str
- 您无法将
与int
-->进行比较这里我选择将随机代码转换为str
str
- 一些打字错误包括一个放错地方的引文,一些我不记得的其他打字错误,还有一个牛津逗号
- 我还没有检查你游戏的逻辑,但是你现在可以运行并测试它了。(请注意,10种颜色对主脑来说太多了,原版有5种,或许6种)
import random
s = [0]*4
s[0] = str(random.randint(0, 9))
s[1] = str(random.randint(0, 9))
s[2] = str(random.randint(0, 9))
s[3] = str(random.randint(0, 9))
board = (s[0] + ' | ' + s[1] + ' | ' + s[2] + ' | ' + s[3])
# user guess a number
u1 = input('Your Name')
pig = 0
cow = 0
match = 0
while match != 4:
uu = int(input("Enter a number 0-9")) #user input
if uu == s[0]:
s[0].replace(uu)
cow += 1
elif uu == s[1]:
s[1].replace(uu)
cow += 1
elif uu == s[2]:
s[2].replace(uu)
cow += 1
elif uu == s[3]:
s[3].replace(uu)
cow += 1
else:
print('Wrong')
pig = 0
continue
print(str(u1) + ' Your cows ' + str(cow) + ", and pigs " + str(pig))
仔细检查
board=…
行上的报价。你能把你看到的整个错误信息都包括进去吗?