Home  >  Article  >  Backend Development  >  How to run a line of code multiple times based on input?

How to run a line of code multiple times based on input?

王林
王林forward
2024-02-22 13:30:12930browse

How to run a line of code multiple times based on input?

Question content

Using python, I'm trying to find a faster way to run randint multiple times based on input without having to write out each input possibility. This is a dice roller for tabletop games. code show as 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'm continuing to add other dice types. Basically I want to be able to put 100 as input and have it give me 100 randint without having to manually code until if i2=="100" but still keep the different "ifs". I also want to print the sum of the rolling values

For the second part, I tried print(sum(value1,value2)) but since value1 and value2 are not integers, I got the error


Correct answer


If I understand correctly, you want to simplify the operation of the algorithm.

My code works as follows.

The user enters the type of dice (e.g. "d6", "d10") and the number of throws. The program checks that the entered die is of the correct type (starts with "d" and has a positive number of sides).

If the data is correct, the program will generate the given number of throws using the given dice, save the results to a list of values ​​and print them.

The program calculates the sum of the obtained results and prints it.

The program asks the user if he wishes to proceed with another throw.

If the user decides to roll again, the program will repeat the process of generating and printing the result.

If the user enters incorrect data (for example, the dice is in the wrong format or the number of rolls is less than 1), the program will print 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.")

The above is the detailed content of How to run a line of code multiple times based on input?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete