1

I'm tying to make it so when you enter a digit value in the while loop, the loop ends and so does the program. However when I type "break" I get an invalid syntax error:(. Sorry for the bad coding, this is my first time.

print('Hello, world!')
print('What is your name?')
myName = input()
print('It is nice to met you, '+ myName)
print('The length of your name is:')
print(len(myName))
print('What is your age?')
myAge=input()
if myAge.isdigit():print('You will be ' + str(int(myAge) + 1) + ' in "one" year')
else: print('that is not a number. Lets try again, what is your age?')
C = 0
while (C<=3):
    myAge2=input()
    C+=1
    if myAge2.isdigit():print('You will be ' + str(int(myAge2) + 1) + ' in "one" year')
    break
    else: print('Try again')
2
  • 7
    Indent breakafter the if - also drop the codeblock after the if and else to a newline Commented Jun 22, 2020 at 5:47
  • Please add the error message. Commented Jun 22, 2020 at 5:47

3 Answers 3

2

Indentation to apply the correct break

 if myAge2.isdigit():
    print('You will be ' + str(int(myAge2) + 1) + ' in "one" year')
    break
 else: 
    print('Try again')
Sign up to request clarification or add additional context in comments.

Comments

0

I tried simplifying the code:

print('Hello, world!\nWhat is your name')
myName = input()
print('It is nice to met you, {}.\nThe length of your name is: {}'.format(myName, len(myName)))
print('What is your age?')
myAge=input()
if myAge.isdigit():
  print('You will be ' + str(int(myAge) + 1) + ' in "one" year')
else: 
  print('that is not a number. Lets try again, what is your age?')

myAge2=input()
while myAge2.isdigit():
  print('You will be ' + str(int(myAge2) + 1) + ' in "one" year')
  break
else:  
  print('Try again')

Comments

0

Problem with the code indentation, otherwise everything is working perfectly.

print('Hello, world!')
print('What is your name?')
myName = input()
print('It is nice to met you, ' + myName)
print('The length of your name is:')
print(len(myName))
print('What is your age?')
myAge = input()
if myAge.isdigit():
    print('You will be ' + str(int(myAge) + 1) + ' in "one" year')
else:
    print('that is not a number. Lets try again, what is your age?')
C = 0
while (C <= 3):
    myAge2 = input()
    C += 1
    if myAge2.isdigit():
        print('You will be ' + str(int(myAge2) + 1) + ' in "one" year')
        break
    else:
        print('Try again')

Output

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.