简体   繁体   English

如何在Python上循环播放?

[英]How would I loop this on Python?

How would I loop this code to make it so that the user inputs the number of friends they have, their names, and in the end, the program will be able to output the information? 我将如何循环执行此代码以使用户输入他们拥有的朋友的数量,他们的名字,最后该程序将能够输出信息? This is what I have so far, but I believe it's incorrect. 到目前为止,这是我所拥有的,但是我认为这是不正确的。

Friends = int(input("Please enter number of friends")
for i in range(Friends):
    Name = input("Please enter friend number 1:")

Append each name to a list, then print the list. 将每个名称附加到列表,然后打印列表。 And use string formatting to put an appropriate number in the prompt. 并使用字符串格式在提示中输入适当的数字。

friendList = []
Friends = int(input("Please enter number of friends")
for i in range(Friends):
    Name = input("Please enter friend number %d: " % (i+1))
    friendList.append(Name)
print(friendList)

Here a try using list comprehension: 这里尝试使用列表理解:

Friends = int(input("Please enter number of friends :"))
Names = [input("Please enter friend number {}:".format(i)) for i in range(1,Friends+1)]
print(Names)

Loop using the number of friends, and store the name for each of them: 循环使用好友数,并存储每个好友的名称:

friend_count = int(input("Please enter number of friends: "))
friend_list = []
for friend_index in range(friend_count):
    name = input("Please enter friend number {}: ".format(friend_index + 1))
    friend_list.append(name)

print(friend_list)

You can use raw_input, from the documentation 您可以使用来自文档的raw_input

If the prompt argument is present, it is written to standard output without a trailing newline. 如果存在提示参数,则将其写入到标准输出中,而无需尾随换行符。 The function then reads a line from input, converts it to a string (stripping a trailing newline), and returns that. 然后,该函数从输入中读取一行,将其转换为字符串(将尾随换行符分隔),然后将其返回。 When EOF is read, EOFError is raised. 读取EOF时,将引发EOFError。

Code

name_array = list()
num_friends = raw_input("Please enter number of friends:")
print 'Enter Name(s): '
for i in range(int(num_friends)):
    n = raw_input("Name :")
    name_array.append((n))
print 'Names: ',name_array

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

相关问题 我将如何循环遍历元素 Python Selenium - How would I loop through elements Python Selenium 我如何制作一个在python 3中创建实例的循环? - How would I make a loop that creates instances in python 3? Python我想知道如何使用for循环编写(DictWriter) - Python i would like to know how to writerow with a for loop (DictWriter) 我将如何尝试并期望块 python 在 for 循环中捕获无限循环 - How would I make a try and expect block python to catch an infinite loop in a for loop 我正在尝试使用 while 循环而不是 for 循环。 我将如何更改此代码以使其在 python 中使用 while 循环工作? - I'm trying to use a while loop instead of a for loop. How would I change this code so that it works using a while loop in python? 我如何循环一个 python 文件以无限期地打开另一个 python 文件? - How would I loop a python file to open another python file indefinitely? 我将如何编写这个用 Python 2 编写的 for 循环,称为 python3 的语法中的字符串插值? - How would I write this for loop written in Python 2 known as string interpolation into syntax for python3? 我将如何执行呢? 蟒蛇 - How would I execute this? Python 我如何摆脱这个无限循环? - How would I get rid of this infinite loop? 我如何将这个for循环制作成流程图? - How would I make this for loop into a flowchart?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM