Python 3.3hello程序

2024-06-06 09:32:03 发布

您现在位置:Python中文网/ 问答频道 /正文

# 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循环的程序和另一个for while循环的程序,我得到了for for循环,但我试图设置要重复多少次。如果可以的话,请帮我,提前谢谢!


Tags: andname程序helloforworldismy
3条回答

Python3.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)
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次问候语。

# 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(),因为我有一个过时的编译器/解释器。

相关问题 更多 >