Have an If-Statement but no 'else' after 'if'. Is the 'else' part necessary? i don't want to change the program (which is supposed to randomise 3 die 50 times and count how many matches there were) unless I need to. Mainly focused on 'else' after if statement
#Program 2#
#create a Yatzhee dice program#
import random
print("--------------------")
print("Yatzhee dice program")
print("--------------------")
print("")
print("Welcome to the Yatzhee Dice Program")
print("In this program the dice will get rolled 50 times and at the end we tell you how many matches you got.")
print("")
# before loop #
rolls = 50
total_matches = 0
# --- loop ---
for i in range(rolls): #randomise 50 times#
die1 = random.randint(1, 6)
die2 = random.randint(1, 6)#chooses a number from 1 to 6#
die3 = random.randint(1, 6)
print(die1, "|", die2, "|", die3)
if die1 == die2 == die3:#If all three are the same.#
print("****MATCH*****")#If statement inside for-loop so it adds up how many matches there were#
total_matches += 1 #increases no. of matches by 1 each time there is a match#
#after loop#
print("")
print("The total number of matches was:", total_matches)#tells user how many matches there were#
source https://stackoverflow.com/questions/70908781/unsure-how-to-add-else-after-my-existing-if-statement-without-changing-program
Comments
Post a Comment