我必须做一个骰子游戏。下面是我到目前为止实际掷骰子部分的代码。
如果骰子的总数是偶数,则必须加上10分。但是如果总数是奇数,则必须减去5分。
import random
print("player 1, please roll dice")
rolldice=input("would you like to roll dice?")
if rolldice=="yes":
roll1 = random.randint(1,6)
roll2=random.randint(1,6)
print("You rolled",roll1)
print("you rolled",roll2)
if roll1+roll2="2,4,6,8,10,12":
print("roll1+roll2+10"):
import random
print("player 1,please roll dies")
rolldice=input("would you like to roll dies?")
if rolldice=="yes":
roll1 = random.randint(1,6)
roll2=random.randint(1,6)
print("You rolled",roll1)
print("you rolled",roll2)
res = roll1 + roll2
if (roll1+roll2) % 2 == 0:
res += 10
else:
res -= 5
print(res):
我在代码中添加了注释,以解释代码中的错误以及修复它们的各种方法
import random #indentation must be fixed in your code
print("player 1,please roll dice")
rolldice=input("would you like to roll dice?")
if rolldice=="yes":
roll1 = random.randint(1,6)
roll2=random.randint(1,6)
print("You rolled",roll1)
print("you rolled",roll2)
if roll1+roll2 %2==0: #here % gives reminder of division,this is an
print(roll1+roll2+10) #easier method or it should be roll1+roll2 in [2,4,6,8,10,12] (the list shouldn't be in quotations, that would mean that it is a string,not a list)
else: # if it isn't an even no, it will obviously be odd, so using else
print(roll1+roll2-5)
另外:您在最后一行的print语句后添加了:
,这是一个语法错误:只有当存在不同缩进级别的文本块时才使用:
。即,对于for
环路、if
、else
、elif
等
import random
print("player 1, please roll dies")
rolldice = input("would you like to roll dies?")
if rolldice == "yes":
roll1 = random.randint(1, 6)
roll2 = random.randint(1, 6)
print("You rolled", roll1)
print("you rolled", roll2)
if (roll1 + roll2) % 2 == 0:
print(roll1 + roll2 + 10)
else:
print(roll1 + roll2 - 5)