-2
# This program says hello and asks for my name.
print('Hello world!')

print('What is your name?')

myName = input()

for Name in myName (1,10):

    print('It is nice to meet you, ' + myName)

我被要求创建一个使用 for 循环的程序和另一个用于 while 循环的程序,我有这个 for 循环,但我试图设置我希望它重复多少次myName。如果可以的话,请帮助我,提前谢谢!

4

3 回答 3

1
# your code goes here
print('Hello world!')

print('What is your name?')

#I use raw_input() to read input instead
myName = raw_input()

#ask user how many times you would like to print the name
print('How many times would you like your name to repeat?')
#raw_input() reads as a string, so you must convert to int by using int()
counter = int(raw_input())

#your main issue is here
#you have to use the range() function to go a certain length for for-loops in Python
for x in range(counter):
    print('It is nice to meet you, ' + myName)

注意:对于您的代码,您应该使用input()而不是 raw_input()。我只使用 raw_input() 因为我有一个过时的编译器/解释器。

于 2013-09-27T18:09:07.163 回答
1

蟒蛇 3.x (3.5)

#Just the hello world
print('Hello world!')

#use input() to let the user assign a value to a variable named myName
myName = input('What is your name?:')

#now let the user assign an integer value to the variable counter for how many times their name should be repeated. Here i am enclosing the input of the user with an int() to tell python that the data type is an integer
counter = int(input('how many times do you want to repeat your name?:'))

#Use the Range function to repeat the names
for a in range(counter):
    print('It is nice to meet you, ' + myName)
于 2016-01-30T17:52:40.750 回答
0
for Name in myName (1,10):
    print('It is nice to meet you, ' + myName)

myName是一个字符串,所以你不能调用它,这就是括号的作用。如果您想重复该名称一定次数,您应该遍历一个范围

for i in range(10):
    print('It is nice to meet you, ' + myName)

这将打印出问候语 10 次。

于 2013-09-27T17:50:47.647 回答