Newb while loop syntax error

This is the place for queries that don't fit in any of the other categories.

Newb while loop syntax error

Postby antihero.mc » Thu Aug 01, 2013 10:55 am

I'm trying to create a simple first time program to calculate fret positions for guitars but im getting syntax errors on a simple while loop to print an alert in case of an invalid number entered by the user. Here's what i have for my loop:

Code: Select all
while True:
   fretCount = input("Enter number of frets:")
   if isinstance ( fretCount, (int) ):
      break
   else:
      print str(fretCount) + " is not a valid number."

Enter number of frets:      22.5


Code: Select all
Traceback (most recent call last):
  File "<input>", line 2, in <module>
  File "<string>", line 1
    while True:
        ^
SyntaxError: invalid syntax


I dont really need this alert I suppose but it's been bugging me and I'm sure it's a newb mistake.
ps: not sure if this is the most stable or efficient or even correct method of coding this loop so any input there would be appreciated also.
Thank you.
antihero.mc
 
Posts: 2
Joined: Thu Aug 01, 2013 10:33 am

Re: Newb while loop syntax error

Postby tnknepp » Thu Aug 01, 2013 6:37 pm

First:
Code: Select all
fretCount = raw_input('BLAH BLAH BLAH') # Use raw_input


Second:
raw_input will never give you an integer, only string, so convert to integer, but this is tricky since you are testing for integerness

Try:

Code: Select all
# Define some number
a = 8.1
>>>int(a)%a
8

a = 8
>>>int(a)%a
0


This provides a nice test case. So, you could do:

Code: Select all
if int(a)%a==0:
    break
else:
    print 'WRONG!  Try again.'
Python: 2.7 via Anaconda
Numpy: 1.7
Pandas: 0.11
OS: Windows 7
IDE: Spyder/IPython
User avatar
tnknepp
 
Posts: 119
Joined: Mon Mar 11, 2013 7:41 pm

Re: Newb while loop syntax error

Postby ochichinyezaboombwa » Thu Aug 01, 2013 7:49 pm

Seems that the problem is with
Code: Select all
    while True:
        ^
SyntaxError: invalid syntax
and has nothing to do with input/ raw_input.
ochichinyezaboombwa
 
Posts: 200
Joined: Tue Jun 04, 2013 7:53 pm

Re: Newb while loop syntax error

Postby stranac » Thu Aug 01, 2013 8:20 pm

The only way I can think of something like that can happen:
Code: Select all
>>> while True:
...    fretCount = input("Enter number of frets:")
...    if isinstance ( fretCount, (int) ):
...       break
...    else:
...       print str(fretCount) + " is not a valid number."
...
Enter number of frets:while True
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
  File "<string>", line 1
    while True
        ^
SyntaxError: invalid syntax
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1111
Joined: Thu Feb 07, 2013 3:42 pm

Re: Newb while loop syntax error

Postby ochichinyezaboombwa » Thu Aug 01, 2013 8:55 pm

LOL!!!!!!!!!
ochichinyezaboombwa
 
Posts: 200
Joined: Tue Jun 04, 2013 7:53 pm

Re: Newb while loop syntax error

Postby antihero.mc » Fri Aug 02, 2013 4:57 am

OK. So it looks like it was a PyCrust problem. Downloaded the latest full release of python and its working now. Thanks for all your help.
antihero.mc
 
Posts: 2
Joined: Thu Aug 01, 2013 10:33 am


Return to General Coding Help

Who is online

Users browsing this forum: buran and 3 guests