Issue
Using Python, I am trying to find a quicker way to run a randint multiple times based on an input without having to write out every single input possibility. This is for a dice roller for tabletop games. Code below
import random
from random import randint
i1=input("what type of die?: ")
i2=input("how many times?:")
roll_again = "yes"
while roll_again == "yes" or roll_again == "y":
if i1=="d6":
if i2=="1":
value1=randint(1,6)
print(value1)
roll_again = input("roll again?")
if i2=="2":
value1=randint(1,6)
value2=randint(1,6)
print(value1,value2)
roll_again = input("roll again?")
if i2=="3":
value1=randint(1,6)
value2=randint(1,6)
value3=randint(1,6)
print(value1,value2,value3)
roll_again = input("roll again?")
if i2=="4":
value1=randint(1,6)
value2=randint(1,6)
value3=randint(1,6)
value4=randint(1,6)
print(value1,value2,value3,value4)
roll_again = input("roll again?")
elif i1=="d4":
if i2=="1":
value1=randint(1,4)
print(value1)
roll_again = input("roll again?")
if i2=="2":
value1=randint(1,4)
value2=randint(1,4)
print(value1,value2)
roll_again = input("roll again?")
if i2=="3":
value1=randint(1,4)
value2=randint(1,4)
value3=randint(1,4)
print(value1,value2,value3)
roll_again = input("roll again?")
if i2=="4":
value1=randint(1,4)
value2=randint(1,4)
value3=randint(1,4)
value4=randint(1,4)
print(value1,value2,value3,value4)
roll_again = input("roll again?")
I am continuing to add other dice types. Basically I want to be able to type 100 as input and it gives me 100 randint without having to manually code up to if i2=="100" but still maintain the different "ifs". I would also like to print the sum of the values rolled
For the second part, I attempted print(sum(value1,value2)) but since value1 and value2 are not integers I get errors
Solution
If I have understood correctly you want to simplify the operation of your algorithm.
My code works as follows.
The user enters the type of dice (e.g., "d6", "d10") and the number of throws. The programme checks that the dice type entered is correct (starts with "d" and the number of dice faces is positive).
If the data is correct, the program generates the given number of throws with the given dice, saves the results to the values list and prints them.
The program calculates the sum of the obtained results and prints it.
The program asks the user if they wish to continue with another throw.
If the user decides to have another throw, the program repeats the process of generating and printing the results.
If the user enters incorrect data (e.g., wrong dice format or number of throws less than 1), the program prints an error message.
I assume you will modify this code to suit your needs
from random import randint
i1 = input("Enter the type of dice (e.g., d6, d4): ")
i2 = int(input("Enter the number of dice rolls: "))
if i1.startswith("d") and i2 > 0:
dice_type = int(i1[1:])
values = []
for _ in range(i2):
values.append(randint(1, dice_type))
print(*values)
print("Sum:", sum(values))
roll_again = input("Roll again? (yes/no): ").lower()
while roll_again == "yes":
values = []
for _ in range(i2):
values.append(randint(1, dice_type))
print(*values)
print("Sum:", sum(values))
roll_again = input("Roll again? (yes/no): ").lower()
else:
print("Invalid input. Please enter a valid dice type and number of rolls.")
Answered By - Giggest
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.