This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2012-04-17 00:06:32

jji7skyline
Scratcher
Registered: 2010-03-08
Posts: 1000+

Help debugging this calculator in python?

Code:

number1 = raw_input("Please enter first number")

op = (raw_input("Please enter an operator, either x, +, - or /."))

number2 = (raw_input("Please enter a second number"))


print number1+operator+number2


if op == *
    print number1*number2
elif op == +
    print number1+number2
elif op == -
    print number1-number2
elif op == /
    print number1/number2
else
    print "operator is not valid"

It says that there is an invalid syntax

Code:

if op == *
         ^
SyntaxError : invalid syntax

I don't know why you say goodbye, I say hello!  big_smile

Offline

 

#2 2012-04-17 03:33:48

slinger
Scratcher
Registered: 2011-06-21
Posts: 1000+

Re: Help debugging this calculator in python?

I don't know any python at all but this would be my guess:
Isn't * part of  a string? Imo you should treat it like a string :3
edit: you should ask on Coders' Shed  wink

Last edited by slinger (2012-04-17 03:34:10)


http://s0.bcbits.com/img/buttons/bandcamp_130x27_blue.png

Offline

 

#3 2012-04-17 05:12:36

scimonster
Community Moderator
Registered: 2010-06-13
Posts: 1000+

Re: Help debugging this calculator in python?

slinger wrote:

I don't know any python at all but this would be my guess:
Isn't * part of  a string? Imo you should treat it like a string :3
edit: you should ask on Coders' Shed  wink

Yep, i was just going to say, it looks like a string.

Code:

number1 = raw_input("Please enter first number")

op = raw_input("Please enter an operator, either x, +, - or /.")

number2 = raw_input("Please enter a second number")


print number1+operator+number2


if op == "*"
    print number1*number2
elif op == "+"
    print number1+number2
elif op == "-"
    print number1-number2
elif op == "/"
    print number1/number2
else
    print "operator is not valid"

And are you sure the else if command is "elif"?

Offline

 

#4 2012-04-17 10:21:35

GameHutSoftware
Scratcher
Registered: 2010-04-24
Posts: 1000+

Re: Help debugging this calculator in python?

you need to import antigravity


Lurking more than posting, but still here.

Offline

 

#5 2012-04-17 10:23:55

scimonster
Community Moderator
Registered: 2010-06-13
Posts: 1000+

Re: Help debugging this calculator in python?

GameHutSoftware wrote:

you need to import antigravity

What does some antigravity library have to do with it? The syntax error was not stringing.

Offline

 

#6 2012-04-17 10:36:58

roijac
Scratcher
Registered: 2010-01-19
Posts: 1000+

Re: Help debugging this calculator in python?

Code:

number1 = raw_input("Please enter first number")

op = (raw_input("Please enter an operator, either x, +, - or /."))

number2 = (raw_input("Please enter a second number"))


print number1 + operator + number2


if op == '*':
    print number1 * number2
elif op == '+':
    print number1 + number2
elif op == '-':
    print number1 - number2
elif op == '/':
    print number1 / number2
else:
    print "operator is not valid"

three things:
you should put '' or "" around strings
you should put ':' after compound statements
you should put space around operators (and read PEP8 when you have time)

good luck!  smile

Offline

 

#7 2012-04-20 21:30:48

GameHutSoftware
Scratcher
Registered: 2010-04-24
Posts: 1000+

Re: Help debugging this calculator in python?

scimonster wrote:

GameHutSoftware wrote:

you need to import antigravity

What does some antigravity library have to do with it? The syntax error was not stringing.

just do it


Lurking more than posting, but still here.

Offline

 

#8 2012-04-21 08:07:25

maxskywalker
Scratcher
Registered: 2008-01-27
Posts: 1000+

Re: Help debugging this calculator in python?

scimonster wrote:

slinger wrote:

I don't know any python at all but this would be my guess:
Isn't * part of  a string? Imo you should treat it like a string :3
edit: you should ask on Coders' Shed  wink

Yep, i was just going to say, it looks like a string.

Code:

number1 = raw_input("Please enter first number")

op = raw_input("Please enter an operator, either x, +, - or /.")

number2 = raw_input("Please enter a second number")


print number1+operator+number2


if op == "*"
    print number1*number2
elif op == "+"
    print number1+number2
elif op == "-"
    print number1-number2
elif op == "/"
    print number1/number2
else
    print "operator is not valid"

And are you sure the else if command is "elif"?

Yes, 'else if' is 'elif' in Python.

Offline

 

#9 2012-04-21 08:08:47

maxskywalker
Scratcher
Registered: 2008-01-27
Posts: 1000+

Re: Help debugging this calculator in python?

GameHutSoftware wrote:

scimonster wrote:

GameHutSoftware wrote:

you need to import antigravity

What does some antigravity library have to do with it? The syntax error was not stringing.

just do it

Um, why?  I've got no idea what Antigravity is in Python, but I've never had problems.

Offline

 

#10 2012-04-21 10:44:02

-GizzardGulp-
Scratcher
Registered: 2010-03-05
Posts: 100+

Re: Help debugging this calculator in python?

A variable cannot be an operator in python. It makes it confused.

EDIT: Wouldn't it make more sense to use a function in the end instead of that jumble of 'if's?

Last edited by -GizzardGulp- (2012-04-21 10:49:35)


http://i1118.photobucket.com/albums/k607/Max_Levine/SnoutmolStudios.png

Offline

 

#11 2012-04-21 11:01:44

Magnie
Scratcher
Registered: 2007-12-12
Posts: 1000+

Re: Help debugging this calculator in python?

maxskywalker wrote:

GameHutSoftware wrote:

scimonster wrote:


What does some antigravity library have to do with it? The syntax error was not stringing.

just do it

Um, why?  I've got no idea what Antigravity is in Python, but I've never had problems.

I just tried it and it opened https://xkcd.com/353/tongue

That's funny. xD

Offline

 

#12 2012-04-21 17:45:23

samtwheels
Scratcher
Registered: 2011-03-20
Posts: 1000+

Re: Help debugging this calculator in python?

Update it to Python 3 while you're at it.

Offline

 

Board footer