简体   繁体   English

如何根据用户输入打印特定次数的内容?

[英]How to print something a specific number of times based on user input?

So I'm a beginner to Python, I don't know many things and I'm stuck on one specific thing.所以我是 Python 的初学者,我不知道很多事情,我被困在一个特定的事情上。

I don't know how to print {variable} number of times stored in choice我不知道如何打印存储在choice {variable}次数

Sorry if this sound stupid对不起,如果这听起来很愚蠢

choice = input("How many time do you want to {something}: ")
{variable} = "{something}"
# End of Variables

if choice_number > 0:
    print({variable})

I'm very sorry, if my question sounds weird English isn't my first language..非常抱歉,如果我的问题听起来很奇怪 英语不是我的母语..

Use a for loop:使用for循环:

choice = input("How many times do you want to print {something}: ")
variable = "{something}"

for i in range(int(choice)):
    print(variable)

We use int(choice) to transform the string that user input gets parsed as, into an integer.我们使用int(choice)将用户输入解析为的字符串转换为整数。 Then we use the built-in function range() to create a list of numbers from zero until that integer, and iterate through them with a variable named i .然后我们使用内置函数range()创建一个从零到该整数的数字列表,并使用名为i的变量迭代它们。 We don't do anything with i (though we could), just print variable once for every iteration.我们不对i任何事情(尽管我们可以),只是为每次迭代打印一次variable

Will print会打印

{something}
{something}
{something}
...

Green Cloak Guy 's magnificent answer is definitely the best if you want to print the same thing on multiple lines.如果您想在多行上打印相同的东西, Green Cloak Guy的宏伟答案绝对是最好的。 For the sake of completeness, I think it would be helpful for you to learn that in python you can multiply a string by a number.为了完整起见,我认为了解在python中可以将字符串乘以数字对您有所帮助。 Let's see an example:让我们看一个例子:

choice = input("How many times do you want to print {something}: ")
variable = "{something}"

print(variable * int(choice))

variable will be printed choice times on the same line.变量将在同一行上打印choice时间。 Your output will be:您的输出将是:

{something}{something}{something}{something}... {某事}{某事}{某事}{某事}...

usually, in many languages, when you wanna do_something() , x times what you do is use a loop.通常,在许多语言中,当您想要do_something()x倍您所做的是使用循环。

for i in range(x):
     do_something()

here:这里:


choice = int(input("How many time do you want to {something}: "))
variable = "{something}"
#} End of Variables

if choice > 0:
    for i in range(choice):
        print(variable)

notice that I did some syntax cleanup注意我做了一些语法清理

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

相关问题 如何通过用户输入的数字多次打印一个字符? - How do I print a character multiple times by a user input number? 如何根据用户输入打印字符串 x 次 - How to print a string x times based on user input 如何根据用户输入在python中打印csv文件的特定行? - how to print a specific line of a csv file in python based on user input? 如何从第二个输入中打印第一个输入的设定次数,该次数也基于赔率或偶数相乘 - How to print first input a set number of times from second input which also multiplies based on odds or even 根据用户输入打印 dict 的特定元素? - Print specific elements of dict based on user input? 如何根据上一个函数的用户输入使下一个输入问题重复多次 - how to make next input question repeat a number of times based on user input from previous function 如何根据用户输入生成随机数1和5,如果玩家猜对了,如果打印错误,则打印正确 - How to generate a random number 1 and 5 based on user input ,If player guesses it right print correct if false print wrong 如何使用用户输入的行数打印图案 - How to print pattern with number of rows input by user 如何使用 map 在一行中根据用户输入打印多个数字 - How to print multiples of a number based on user input in one line using map 如何输入一个数字x并打印x次,每次增加2 - How to input a number x and print it x times, increasing by 2 each time
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM