Python Forum
A problem with defining variables
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
A problem with defining variables
#1
I'm trying to create a function that accepts two numbers and returns the product or the number zero if the result is negative.
I wrote the following code

def mul_2nums():
    x = raw_input('write first number: ')
    y = raw_input('write second number: ')
    return x, y


def multiply():
    x = b
    y = a
    print a * b
    if a * b < 0:
        print 0

multiply(x,y)
And i get - NameError: name 'x' is not defined

What am I doing wrong
?
Reply
#2
In mul_2nums you ask the user for 2 numbers and then return the user input - that's good.
The problems:
You need to call this function and assign the 2 values that it will return to two variable names for later use in multiply(). Check the code from your other thread from almost a year ago.
When you define multiply() you need to "tell" python to expect two numbers as arguments.
In order to be able to multiply the two numbers you need to convert the user input (which will be str) to int. The best place to do so is within mul_2nums, i.e. you can keep asking user for input until you get twice valid input that you can convert to int.
Finally - you use python2. You should be using and learning python3. Python2 supports ends at the end of the 2019
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Thank's buran

Can you show an example of how the code that convert the user input to int should look?
Reply
#4
You can use int() built-in function

x = int(raw_input('write first number: '))
this is basic example. it will raise and exception in case of invalid input
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
(Jan-30-2019, 12:14 PM)meru120 Wrote: Thank's buran

Can you show an example of how the code that convert the user input to int should
look?

wanna discuss something?
Reply
#6
what's your question? why you resurrect this thread?
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Problem defining a variable rix 6 3,135 Dec-17-2019, 11:34 AM
Last Post: rix
  problem using exec to save local variables dkarl 0 1,761 Dec-01-2019, 08:52 AM
Last Post: dkarl
  Problem with variables JCB 3 2,251 Nov-13-2019, 07:20 PM
Last Post: the_ophidian_order
  Problem with defining a function snow_y 4 3,153 Nov-26-2018, 02:13 AM
Last Post: snippsat
  Problem with variables Steffenwolt 4 3,240 Jul-14-2018, 07:27 PM
Last Post: Steffenwolt
  Defining Variables in outside modules Barnettch3 4 3,514 Dec-12-2017, 10:19 PM
Last Post: Larz60+
  [split] Problem using global variables RedSkeleton007 1 2,623 Nov-10-2017, 09:13 PM
Last Post: sparkz_alot
  Vending Machine Program: Problem with variables icabero0225 2 8,476 Jun-08-2017, 11:04 AM
Last Post: buran
  MySQLdb, problem with query with user-defined variables buran 6 6,318 Feb-03-2017, 06:16 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020