How to run a line of code multiple times based on input?
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Python is suitable for data science, web development and automation tasks, while C is suitable for system programming, game development and embedded systems. Python is known for its simplicity and powerful ecosystem, while C is known for its high performance and underlying control capabilities.

Python excels in gaming and GUI development. 1) Game development uses Pygame, providing drawing, audio and other functions, which are suitable for creating 2D games. 2) GUI development can choose Tkinter or PyQt. Tkinter is simple and easy to use, PyQt has rich functions and is suitable for professional development.

You can learn basic programming concepts and skills of Python within 2 hours. 1. Learn variables and data types, 2. Master control flow (conditional statements and loops), 3. Understand the definition and use of functions, 4. Quickly get started with Python programming through simple examples and code snippets.

Python is easier to learn and use, while C is more powerful but complex. 1. Python syntax is concise and suitable for beginners. Dynamic typing and automatic memory management make it easy to use, but may cause runtime errors. 2.C provides low-level control and advanced features, suitable for high-performance applications, but has a high learning threshold and requires manual memory and type safety management.

To maximize the efficiency of learning Python in a limited time, you can use Python's datetime, time, and schedule modules. 1. The datetime module is used to record and plan learning time. 2. The time module helps to set study and rest time. 3. The schedule module automatically arranges weekly learning tasks.

Python is better than C in development efficiency, but C is higher in execution performance. 1. Python's concise syntax and rich libraries improve development efficiency. 2.C's compilation-type characteristics and hardware control improve execution performance. When making a choice, you need to weigh the development speed and execution efficiency based on project needs.

Python excels in automation, scripting, and task management. 1) Automation: File backup is realized through standard libraries such as os and shutil. 2) Script writing: Use the psutil library to monitor system resources. 3) Task management: Use the schedule library to schedule tasks. Python's ease of use and rich library support makes it the preferred tool in these areas.

Python is widely used in the fields of web development, data science, machine learning, automation and scripting. 1) In web development, Django and Flask frameworks simplify the development process. 2) In the fields of data science and machine learning, NumPy, Pandas, Scikit-learn and TensorFlow libraries provide strong support. 3) In terms of automation and scripting, Python is suitable for tasks such as automated testing and system management.
