Created a rolling dice in python, but I want the user to be able to input either one out of 2 possible inputs in order to roll the dice
Question:
I’m new to coding so apologies for the nooby question. I’ve written a program that selects a random integer between 2 and 12 (like a pair of 6-sided dice) when the user inputs "r". I want the user to be able to input either a lowercase "r" or an uppercase "R" to achieve the same outcome but I can’t figure out how to do it. The same applies to the rest of the code, when the user is asked if they would like to roll again using "y" or "n". Also I think the code as a whole is probably much longer than it needs to be. Please educate me on how to optimise it and get the same results.
import random
def roll():
roll_dice = input("Enter R to roll the dice. Enter X to exit ")
while roll_dice != "x":
if roll_dice != "r":
roll_dice = input("Invalid input. Try again ")
else:
result = random.randint(2,12)
print(result)
break
return result
def dice():
result = roll()
roll_dice = input("Would you like to roll again? (Y/N) ")
while roll_dice != "n":
if roll_dice == "y":
result = random.randint(2,12)
print(result)
roll_dice = input("Would you like to roll again? (Y/N) ")
else:
roll_dice = input("That is an invalid answer. Please try again ")
print("You have finished rolling")
dice()
Answers:
You can check for multiple conditions in the if statement:
if roll_dice != "r" and roll_dice != "R":
or you can convert the input to lowercase:
roll_dice = input("Enter R to roll the dice. Enter X to exit ")
roll_dice = roll_dice.lower()
As for tips to improve the code, I would suggest the following:
- Use descriptive names so you can immediately see what something is/does without looking at the logic. For example, split the if statement and the condition by first giving the condition a descriptive name.
- Keep functions compact. They should really just do one thing.
- Try not to use
else
when having an if
statement to prevent nesting of logic.
For example:
import random
def get_dice_roll_results(number_of_dice=1, number_of_faces=6):
roll_results = []
for index in range(number_of_dice):
roll_results.append(random.randint(1, number_of_faces))
return roll_results
def run():
total_roll_count = 0
while True:
query = "Would you like to roll the dice? (y/n)n"
is_rolling_again = total_roll_count > 0
if is_rolling_again:
query = "nWould you like to roll the dice again? (y/n)n"
input_value = input(query).lower()
answer_is_invalid = input_value != "y" and input_value != "n"
if answer_is_invalid:
print("That is an invalid answer. Please try again.")
continue
should_role_dice = input_value.lower() == "y"
if not should_role_dice:
break
roll_results = get_dice_roll_results(2, 6)
total_roll_count+=1
result_template = "You rolled ({individual_dice}) totalling {total}!"
formatted_result = result_template.format(
total=sum(roll_results),
individual_dice=", ".join(str(roll) for roll in roll_results)
)
print(formatted_result)
print("nYou have finished rolling!")
run()
use some condition checking
if roll_dice != "r" and roll_dice != "R":
As I was composing an answer along with a solution to your issue, I see that similar answers have been posed. I will still present my suggestion for a solution that parallels previous comments and that is the use of the "lower()" string function. You can find multiple references to its usage, but here is one that you might reference String lowercase.
Utilizing this capability within Python following is a possible refactored version of your code.
import random
def roll():
result = 2
roll_dice = input("Enter R to roll the dice. Enter X to exit ")
while roll_dice.lower() != "x":
if roll_dice.lower() != "r":
roll_dice = input("Invalid input. Try again ")
else:
result = random.randint(2,12)
print(result)
break
return result
def dice():
result = roll()
roll_dice = input("Would you like to roll again? (Y/N) ")
while roll_dice.lower() != "n":
if roll_dice.lower() == "y":
result = random.randint(2,12)
print(result)
roll_dice = input("Would you like to roll again? (Y/N) ")
else:
roll_dice = input("That is an invalid answer. Please try again ")
print("You have finished rolling")
dice()
Testing this refactored code produced the following test results at the terminal.
@Vera:~/Python_Programs/Dice$ python3 Roll.py
Enter R to roll the dice. Enter X to exit r
11
Would you like to roll again? (Y/N) y
6
Would you like to roll again? (Y/N) n
You have finished rolling
All of the answers are constructive and should assist you. See if this refactored code meets the spirit of your project.
I’m new to coding so apologies for the nooby question. I’ve written a program that selects a random integer between 2 and 12 (like a pair of 6-sided dice) when the user inputs "r". I want the user to be able to input either a lowercase "r" or an uppercase "R" to achieve the same outcome but I can’t figure out how to do it. The same applies to the rest of the code, when the user is asked if they would like to roll again using "y" or "n". Also I think the code as a whole is probably much longer than it needs to be. Please educate me on how to optimise it and get the same results.
import random
def roll():
roll_dice = input("Enter R to roll the dice. Enter X to exit ")
while roll_dice != "x":
if roll_dice != "r":
roll_dice = input("Invalid input. Try again ")
else:
result = random.randint(2,12)
print(result)
break
return result
def dice():
result = roll()
roll_dice = input("Would you like to roll again? (Y/N) ")
while roll_dice != "n":
if roll_dice == "y":
result = random.randint(2,12)
print(result)
roll_dice = input("Would you like to roll again? (Y/N) ")
else:
roll_dice = input("That is an invalid answer. Please try again ")
print("You have finished rolling")
dice()
You can check for multiple conditions in the if statement:
if roll_dice != "r" and roll_dice != "R":
or you can convert the input to lowercase:
roll_dice = input("Enter R to roll the dice. Enter X to exit ")
roll_dice = roll_dice.lower()
As for tips to improve the code, I would suggest the following:
- Use descriptive names so you can immediately see what something is/does without looking at the logic. For example, split the if statement and the condition by first giving the condition a descriptive name.
- Keep functions compact. They should really just do one thing.
- Try not to use
else
when having anif
statement to prevent nesting of logic.
For example:
import random
def get_dice_roll_results(number_of_dice=1, number_of_faces=6):
roll_results = []
for index in range(number_of_dice):
roll_results.append(random.randint(1, number_of_faces))
return roll_results
def run():
total_roll_count = 0
while True:
query = "Would you like to roll the dice? (y/n)n"
is_rolling_again = total_roll_count > 0
if is_rolling_again:
query = "nWould you like to roll the dice again? (y/n)n"
input_value = input(query).lower()
answer_is_invalid = input_value != "y" and input_value != "n"
if answer_is_invalid:
print("That is an invalid answer. Please try again.")
continue
should_role_dice = input_value.lower() == "y"
if not should_role_dice:
break
roll_results = get_dice_roll_results(2, 6)
total_roll_count+=1
result_template = "You rolled ({individual_dice}) totalling {total}!"
formatted_result = result_template.format(
total=sum(roll_results),
individual_dice=", ".join(str(roll) for roll in roll_results)
)
print(formatted_result)
print("nYou have finished rolling!")
run()
use some condition checking
if roll_dice != "r" and roll_dice != "R":
As I was composing an answer along with a solution to your issue, I see that similar answers have been posed. I will still present my suggestion for a solution that parallels previous comments and that is the use of the "lower()" string function. You can find multiple references to its usage, but here is one that you might reference String lowercase.
Utilizing this capability within Python following is a possible refactored version of your code.
import random
def roll():
result = 2
roll_dice = input("Enter R to roll the dice. Enter X to exit ")
while roll_dice.lower() != "x":
if roll_dice.lower() != "r":
roll_dice = input("Invalid input. Try again ")
else:
result = random.randint(2,12)
print(result)
break
return result
def dice():
result = roll()
roll_dice = input("Would you like to roll again? (Y/N) ")
while roll_dice.lower() != "n":
if roll_dice.lower() == "y":
result = random.randint(2,12)
print(result)
roll_dice = input("Would you like to roll again? (Y/N) ")
else:
roll_dice = input("That is an invalid answer. Please try again ")
print("You have finished rolling")
dice()
Testing this refactored code produced the following test results at the terminal.
@Vera:~/Python_Programs/Dice$ python3 Roll.py
Enter R to roll the dice. Enter X to exit r
11
Would you like to roll again? (Y/N) y
6
Would you like to roll again? (Y/N) n
You have finished rolling
All of the answers are constructive and should assist you. See if this refactored code meets the spirit of your project.