-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)

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!

Jon Clements
  • 138,671
  • 33
  • 247
  • 280
iExodus
  • 11
  • 1
  • 1
  • 4

3 Answers3

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)

Note: For your code, you should use input() instead of raw_input(). I only used raw_input() because I have an outdated compiler/interpreter.

1

python 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)
Gajendra D Ambi
  • 3,832
  • 26
  • 30
0
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 call it, 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:

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

This will print out the greeting 10 times.

poke
  • 369,085
  • 72
  • 557
  • 602