I have to make a dice game. Down below is my code so far for the actual dice rolling section.
If the total number from the dices are even, 10 points must be added on. However if the total is an odd number, 5 points must be taken away.
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):
I've added comments in my code to explain the mistakes in your code and various methods to fix them
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)
also: you've added :
after a print statement in the last line, that is a syntax error: :
is used only when there is a block of text at a different indentation level. ie, for for
loops, if
, else
, elif
etc
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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.