Python 3.3 你好程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19056962/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python 3.3 Hello program
提问by iExodus
# 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)
I was asked to create a program that uses a for loop and another program for while loop, I've got this for for loop but I'm trying to set how many times I want it to repeat myName
. Please help me out if you can, thanks in advance!
我被要求创建一个使用 for 循环和另一个程序 for while 循环的程序,我有这个 for for 循环,但我正在尝试设置我希望它重复的次数myName
。如果可以,请帮助我,在此先感谢!
回答by poke
for Name in myName (1,10):
print('It is nice to meet you, ' + myName)
myName
is a string, so you won't be able to callit, which is what the parentheses do. If you want to repeat the name for a certain amount of times, you should iterate over a range:
myName
是一个字符串,因此您将无法调用它,这就是括号的作用。如果你想重复这个名字一定次数,你应该迭代一个范围:
for i in range(10):
print('It is nice to meet you, ' + myName)
This will print out the greeting 10 times.
这将打印出问候语 10 次。
回答by But I'm Not A Wrapper Class
# 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)
Note: For your code, you should use input()instead of raw_input(). I only used raw_input() because I have an outdated compiler/interpreter.
注意:对于您的代码,您应该使用input()而不是 raw_input()。我只使用 raw_input() 因为我有一个过时的编译器/解释器。
回答by Gajendra D Ambi
python 3.x (3.5)
蟒蛇 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)