简体   繁体   English

如何在循环中打印特定数量的输入语句?

[英]How do I print a specific number of input statements in a loop?

I'm writing a code for my class but I'm having a little trouble at one part. 我正在为我的班级写一个代码,但我在一个部分遇到了一些麻烦。 I'm having the user input a number and then I need a loop to print specific statements based off the number the user inputted. 我正在让用户输入一个数字然后我需要一个循环来根据用户输入的数字打印特定的语句。 So for example: 例如:

def main():
    totalnumber = input("Enter the number of circles: ")
    i = 0
    for i in totalnumber:
        i = 0 + 1
        value = input("Enter the radius of circle",str(i)+":")

So I basically need the output to look like: 所以我基本上需要输出看起来像:

Enter the number of circles: 3
Enter the radius of circle 1:
Enter the radius of circle 2:
Enter the radius of circle 3:

I'm getting the error 我收到了错误

TypeError: input expected at most 1 arguments, got 2

Is what I'm doing above okay to do or should I use a different approach? 我上面做的是好的还是我应该采用不同的方法? If its okay what is wrong within my code that would be giving me that sort of error? 如果它在我的代码中有什么问题可以给我那种错误呢?

Your for loop doesn't look correct. 你的for循环看起来不正确。 Try 尝试

for number in range(int(totalnumber)):
    i = number+1
    value = input("Enter the radius of circle"+str(i)+":")

Try: 尝试:

def main():
    total_number = input("Enter the number of circles: ")
    for number in range(1, int(total_number) + 1):
        value = input("Enter the radius of circle {}: ".format(number))

main()

First: you need to convert the input to int , then iterate it by the number. 首先:您需要将输入转换为int ,然后按数字迭代。

Notes: 笔记:

  • use python pep-8 when naming your parameters user _ between the names 在名称之间命名参数user _时使用python pep-8
  • string formating is best with format try to use it. 字符串格式最好用格式尝试使用它。

暂无
暂无

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

相关问题 如何根据用户输入显示if语句? - How do I get if statements to print based on user input? 如何从python中的for循环中打印特定项目? - How do I print a specific item from a for loop in python? 如何通过用户输入的数字多次打印一个字符? - How do I print a character multiple times by a user input number? 如何合并这些打印报表? - How do i combine these print statements? 如何在 python 循环(for 或 while)中运行多个打印语句“ONCE”, - How do I run multiple print statements “ONCE” in a python loop (for or while), 如何让计算机处理循环,然后在python中以另一种方式迭代打印语句? - How do I make the computer process the loop, then iterate the print statements the other way round in python? 如何使用打印语句创建菜单? 我希望能够选择一个数字,并根据我选择的数字运行该功能 - How do i create a menu using print statements? I want to be able to choose a number and based on what number i choose run that function 如何基于Python中的特定用户输入循环条件语句? - How to loop conditional statements based on specific user input in Python? 如何遍历 Dataframe 中的行并根据条件语句对特定行执行计算? - How do I loop over rows in a Dataframe and perform calculations on specific rows based on conditional statements? 在 SLURM 中运行程序时如何保存打印语句? - How do I save print statements when running a program in SLURM?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM