Python Forum

Full Version: SyntaxError: Invalid syntax in a while loop
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello everyone,
I would like to create a list of 12 numbers which each term is equal to the previous triple using a while loop.
Here is my code:
i, n=int(input("Enter a number:"), 1
while n<=12:
    print(i, end=",")
    i*=3
    n+=1
Error:
File "learning.py", line 2 while n<=1: ^ SyntaxError: invalid syntax
I don't understand why it doesn't work, what should I do differently ?
i, n=int(input("Enter a number:"), 1
is invalid python syntax, the error is showing up on line 2 because of line 1 error

use something like:
try:
    n = int(input('Enter starting number: '))
    for i in range(12):
        print('{}, '.format(n), end = '')
        n = n * 3
except ValueError:
    print("Numbers only, please")
Thank you very much for the quick awnser and for your code. I'll check it!
Meanwhile I've found the error, it was a parenthesis missing in line 1.
Here is the script
n, i= 1, int(input("Enter a number:"))
while n<=12:
    if n!=12:
        print(i, end=", ")
    else:
        print(i)
    i*=3
    n+=1
It works !
Output:
Enter a number:1 1, 3, 9, 27, 81, 243, 729, 2187, 6561, 19683, 59049, 177147
Great!