简体   繁体   English

如何使用 python 通过用户输入在范围内打印一定数量的结果?

[英]How do you print certain amount of results in range by user input using python?

I was trying to create a dice rolling program where the user enters how much a certain amount of dice they want to roll.我试图创建一个骰子滚动程序,用户可以在其中输入他们想要滚动的一定数量的骰子。 But it is not working.但它不起作用。 What should I do?我应该怎么办?

from random import branding
repeat = True
while repeat:
    amount = input('how many dice do you want to roll?')
    for i in range(0, amount):
       print("You rolled",randint(1,6))
    print("Do you want to roll again?")
    repeat = ("y" or "yes") in input().lower()

I am new to this language, but I love it and I bet you do too, hence I put together an idea based on your code, I certainly hope this answers your question.我是这门语言的新手,但我喜欢它,我打赌你也喜欢,因此我根据你的代码提出了一个想法,我当然希望这能回答你的问题。 Keep coding bro, Python is great!继续编码兄弟,Python 很棒!

#First, you only need the random function to get the results you need :)
import  random
#Let us start by getting the response from the user to begin
repeat = input('Would you like to roll the dice [y/n]?\n')

#As long as the user keeps saying yes, we will keep the loop
while repeat != 'n':
# How many dices does the user wants to roll, 2 ,3 ,4 ,5  who knows. let's ask!
    amount = int(input('How many dices would you like to roll? \n'))
# Now let's roll each of those dices and get their results printed on the screen
    for i in range(0, amount):
       diceValue = random.randint(1, 6)
       print(f"Dice {i+1} got a [{diceValue}] on this turn.")
#Now, let's confirm if the user still wants to continue playing.       
    repeat = input('\nWould you like to roll the dice [y/n]?\n')

# Now that the user quit the game, let' say thank you for playing
print('Thank you for playing this game, come back soon!')
# Happy Python Coding, buddy! I hope this answers your question.

On the 4th line, you just did input() .在第 4 行,您刚刚做了input() However, you need to add an int() function around the input.但是,您需要在输入周围添加一个int() function。

This would be your code:这将是您的代码:

from random import *
repeat = True
while repeat:
    amount = int(input('how many dice do you want to roll?'))
    for i in range(0, amount):
       print("You rolled",randint(1,6))
    print("Do you want to roll again?")
    repeat = ("y" or "yes") in input().lower()

Hope this helps!希望这可以帮助!

  1. You're importing branding from the random module, not randint .您正在从random模块导入branding ,而不是randint

  2. Use int() when calling the input() function, for the range() function to work properly.调用input() function 时使用int() ,以使range() function 正常工作。

  3. In the last line, repeat = ("y" or "yes") in input().lower() , ("y" or "yes") will evaluate to a boolean, and then your check will become something like True in input().lower() which gives incorrect, and bizarre results.在最后一行, repeat = ("y" or "yes") in input().lower()("y" or "yes")将评估为 boolean,然后您的检查将变为True in input().lower()给出不正确和奇怪的结果。 Use something like repeat = input().lower() in ("y", "yes") instead. repeat = input().lower() in ("y", "yes")东西。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python:如何获得一个程序来提示用户一定的固定时间来输入用户信息? - Python: How do you get a programme to prompt the user a certian fixed amount of times for a user input? 你如何让 Python 向用户询问输入“x”的次数? - How do you get Python to ask the user for an input 'x' amount of times? 如何根据用户在 python 中的输入打印列表中的某些项目? - How do i print certain items in a list based on user input in python? 如何将字符串中的一定数量的零替换为要求用户输入的数量,从字符串的末尾开始? - How do I replace a certain amount of zeros in a string with an amount that the user is asked to input, starting from the end of the string? 您如何在python中创建一个自定义异常来处理用户输入,并考虑帐户中的值范围? - How do you create a custom exception that handles user input taking range of values in account, in python? 如何在python中从多行用户输入中打印最大和最小数字? - How do you print the maximum and minimum numbers from multiple lines of user input in python? 我如何从列表中随机选择用户要求在 python 中使用输入的次数? - how do i randomly pick characters out of a list the amount of times the user asks for using input in python? 如何在多行上将用户输入打印到 csv 文件? - How do you print user input to csv files on multiple lines? 时间一到,如何给用户一定的时间来回复和打印消息? (Python) - How to give the user certain amount of time to answer and print a message as soon as the time expires? (Python) 如何在python的输入中打印“剩余金额” - How to print the "remaining amount" inside the input in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM