Help with code

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

Help with code

Postby helpmepass » Wed Oct 30, 2013 7:24 pm

Getting a syntax error. If someone could make the corrections, I'd appreciate the help thanks. This is the code.

Code: Select all
import random

# main module
def main():
    # Local variables
    num1 = 0
    num2 = 0
    correctAnswer = 0

    # Get numbers
    num1 = get_numbers()
    num2 = get_numbers()

    # Correct answer and user answer
    correctAnswer = (num1 + num2)
    userAnswer = input(num1' plus ' num2' is: ')

    #Display result
    output_sum()

# The get_numbers function generates two random numbers to be added.
def get_numbers():
    # Local variable
    num = random.randint(1, 999)

# The output_sum function shows the answer and congratulates the user if correct.
def output_sum():
          if correctAnswer == userAnswer:
                       print ('Congratulations! You are correct!')
          else:
                       print ('Wrong. The correct answer was: ' correctAnswer )
Last edited by Yoriz on Wed Oct 30, 2013 8:05 pm, edited 1 time in total.
Reason: First post lock, Added code tags
helpmepass
 
Posts: 8
Joined: Wed Oct 30, 2013 7:09 pm

Re: Help with code

Postby Yoriz » Wed Oct 30, 2013 8:07 pm

Hi, Welcome to the forum.
Please read the 'new user read this' link in my signature.
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 571
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: Help with code

Postby dboxall123 » Wed Oct 30, 2013 8:21 pm

In your print statements you need to include commas to seperate the strings from the variables.
dboxall123
 
Posts: 108
Joined: Fri Jul 12, 2013 5:28 pm

Re: Help with code

Postby Marbelous » Wed Oct 30, 2013 10:23 pm

You have a couple more problems here than a syntax error:

1) You define main() but never call it. :oops:
2) You define get_numbers() with a local variable (num) which is a bad idea in general, but then you don't even use the local. Just return the value from your function.

Try to handle these issues and repost. Pretty good attempt overall though. :)
Marbelous
 
Posts: 66
Joined: Fri May 31, 2013 8:12 pm

Re: Help with code

Postby helpmepass » Thu Oct 31, 2013 5:31 pm

Code: Select all
input(num1' plus ,num2, ' is: )

See any issues here?
helpmepass
 
Posts: 8
Joined: Wed Oct 30, 2013 7:09 pm

Re: Help with code

Postby metulburr » Thu Oct 31, 2013 5:40 pm

New Users, Read This
version Python 3.3.2 and 2.7.5, tkinter 8.5, pyqt 4.8.4, pygame 1.9.2 pre
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
User avatar
metulburr
 
Posts: 1122
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Help with code

Postby Marbelous » Thu Oct 31, 2013 5:59 pm

Yes, code tags are very important here because they preserve the indentation. Also post you complete error traceback message rather than just telling us "syntax error".

I suspect that the error you're getting is because you have not passed any arguments to your output_sum function. It cannot read variables outside its scope so it doesn't know what correctAnswer and userAnswer are.

For example:
Code: Select all
def func1(argument1, argument2):
     "Do stuff with arguments"
     return result
Marbelous
 
Posts: 66
Joined: Fri May 31, 2013 8:12 pm

Re: Help with code

Postby helpmepass » Thu Oct 31, 2013 6:26 pm

Where would I use a code tag in my code?

Also, there's no big error message. All it says is "invalid syntax"
helpmepass
 
Posts: 8
Joined: Wed Oct 30, 2013 7:09 pm

Re: Help with code

Postby Yoriz » Thu Oct 31, 2013 7:25 pm

helpmepass wrote:Where would I use a code tag in my code?

Also, there's no big error message. All it says is "invalid syntax"


My post asked you to read this link
New Users, Read This
It details how to use code tags, what to include in your questions ect ect.
Please have a read, it will help you get better answers to your questions.
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 571
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: Help with code

Postby dboxall123 » Thu Oct 31, 2013 7:33 pm

Code: Select all
input(num1' plus ,num2, ' is: )

will cause syntax error. Should be
Code: Select all
input(num1,"plus",num2,"is: ")
dboxall123
 
Posts: 108
Joined: Fri Jul 12, 2013 5:28 pm

Re: Help with code

Postby helpmepass » Thu Oct 31, 2013 7:39 pm

I'm still getting an invalid syntax massage but now the semicolon is highlighted in red
helpmepass
 
Posts: 8
Joined: Wed Oct 30, 2013 7:09 pm

Re: Help with code

Postby dboxall123 » Thu Oct 31, 2013 7:53 pm

Post your entire code. Make sure you highlight the code and then click on the code button.
dboxall123
 
Posts: 108
Joined: Fri Jul 12, 2013 5:28 pm

Re: Help with code

Postby Marbelous » Thu Oct 31, 2013 8:02 pm

helpmepass wrote:
Code: Select all
input(num1' plus ,num2, ' is: )

See any issues here?

I'm using Python 2.7 and I'm not familiar with this use of input(). Maybe just simplify it into two lines:

Code: Select all
num1 = input('enter num1: ')
num2 = input('enter num2: ')


Also, now that you're using code tags, please post your whole code again. Add the error traceback by copy/pasting the whole thing so we can see where the compiler is pointing out the error.
Marbelous
 
Posts: 66
Joined: Fri May 31, 2013 8:12 pm

Re: Help with code

Postby dboxall123 » Thu Oct 31, 2013 8:07 pm

Your right Marbelous, I didn't realise that input could only take one argument.
dboxall123
 
Posts: 108
Joined: Fri Jul 12, 2013 5:28 pm

Re: Help with code

Postby Marbelous » Thu Oct 31, 2013 8:12 pm

Ahh, I think I know what you're doing. I've actually never tried putting variables into the input prompt string but obviously the simple way you're trying doesn't work (it was my first thought too.) You have to do it with the string formatting symbol (%). Like this:

Code: Select all
userAnswer = input("%d plus %d = " % (num1, num2))


EDIT: Just to further clarify, here's how to format some variables into a string:
Code: Select all
>>> print "A string: %s, A Decimal: %d, A Float: %f" % ("STRINGY", 256, 256)
A string: STRINGY, A Decimal: 256, A Float: 256.000000

## You use a Tuple of values (or variables) to be formatted into the placeholders (e.g %s and %d mean string and decimal)
Marbelous
 
Posts: 66
Joined: Fri May 31, 2013 8:12 pm

Re: Help with code

Postby helpmepass » Thu Oct 31, 2013 11:38 pm

Ok, I got past the syntax error but surprise. Another mistake.

This is the code
Code: Select all
def main():
    # Local variables
    num1 = 0
    num2 = 0
    correctAnswer = 0

    # Get numbers
    num1 = get_numbers()
    num2 = get_numbers()

    # Correct answer and user answer
    correctAnswer = (num1 + num2)
    userAnswer = print(num1, "plus",num2,"is ")
    input ()
                       

    #Display result
    output_sum()

# The get_numbers function generates two random numbers to be added.
def get_numbers():
    # Local variable
    num = random.randint(1, 999)
    return num

# The output_sum function shows the answer and congratulates the user if correct.
def output_sum():
          if correctAnswer == userAnswer:
                       print ('Congratulations! You are correct!')
          else:
                       print ('Wrong. The correct answer was: ', correctAnswer )

# Call the main function
main()

And this is the error message:
Code: Select all
Traceback (most recent call last):
  File "C:\Python33\EdwardStaplesCh6Ex2.py", line 43, in <module>
    main()
  File "C:\Python33\EdwardStaplesCh6Ex2.py", line 27, in main
    output_sum()
  File "C:\Python33\EdwardStaplesCh6Ex2.py", line 37, in output_sum
    if correctAnswer == userAnswer:
NameError: global name 'correctAnswer' is not defined
>>>
Last edited by micseydel on Thu Oct 31, 2013 11:42 pm, edited 1 time in total.
Reason: Code tags.
helpmepass
 
Posts: 8
Joined: Wed Oct 30, 2013 7:09 pm

Re: Help with code

Postby micseydel » Thu Oct 31, 2013 11:50 pm

This is the sixth time you're being told to use code tags, please do so.

If the resources my fellow moderators have provided you are inadequate, you must communicate with us about it. If you cannot follow instructions in a forum which is trying to help you there is no way you will do more than fail at the task you're asking about. We want to you to be successful.

Why am I being so aggressive about this? Because Python without indentation (which code tags preserve) is gibberish. You waste my time editing your post so that non-moderators can help you as well as your own time and non-moderators who visit this post and see that they can't help you* before I get around to it. Again, we want to help you, please help us to do so.


* Note that non-moderators can quote the post and are just as able as moderators to retrieve the proper code (sometimes there isn't even indentation there, but there usually is, and was here). But the OP should not be relying on this knowledge or requiring the extra work on the part of our already generous members.

Also note that my original post said "third time" but looking over it it's at least six now. Three is unacceptable.
Join the #python-forum IRC channel on irc.freenode.net!
User avatar
micseydel
 
Posts: 939
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Help with code

Postby helpmepass » Fri Nov 01, 2013 12:06 am

Yeah, I figured it out just now. But I still need help.
helpmepass
 
Posts: 8
Joined: Wed Oct 30, 2013 7:09 pm

Re: Help with code

Postby stranac » Fri Nov 01, 2013 12:20 am

You're using a variable that's not defined.
If you want to use a variable in a function, either define it, or pass it as an argument.

Reading a basic python tutorial would help.
Friendship is magic!

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

Re: Help with code

Postby helpmepass » Fri Nov 01, 2013 12:25 am

I did define it though. It's num1+num2.
helpmepass
 
Posts: 8
Joined: Wed Oct 30, 2013 7:09 pm

Next

Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 2 guests