Python Forum

Full Version: help (variables) - NameError: name 'Payment' is not defined?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am new to python but why is this code not working?
#Parking Ticket machine
Money = 50

import time
print('''
----------------------------
|Press Enter To Continue   |
|                          |
|                          |
|                          |
|                          |
|               Made by Ben|
----------------------------
''')
input("")
print('''
----------------------------
|How Long Are you Staying? |
| 1 = 1 hour         £1.20 |
| 2 = 2 hours        £2.40 |
| 3 = 3 hours        £3.00 |
| 4 = All Day        £7.00 |
| 5 = All week       £14.00|
----------------------------
''')
In = input (">")
if In == 1:
    Payment = 1.20

if In == 2:
    Payment = 2.40

if In == 3:
    Payment = 3.00

if In == 4:
    Payment = 7.00

if In == 5:
    Payment = 14.00
print(Payment)
print(f"Your current statement is £{Money}")
Error:
Traceback (most recent call last):
File "C:/Users/bcola/Desktop/Other/python/app.py", line 41, in <module>
print(Payment)
NameError: name 'Payment' is not defined


Please help
input will return str and you are comparing it to int. That is why none of the if statements is executed and Payment is not defined
tried this but still not working help.
#Parking Ticket machine
Money = 50

import time
print('''
----------------------------
|Press Enter To Continue   |
|                          |
|                          |
|                          |
|                          |
|               Made by Ben|
----------------------------
''')
input("")
print('''
----------------------------
|How Long Are you Staying? |
| 1 = 1 hour         £1.20 |
| 2 = 2 hours        £2.40 |
| 3 = 3 hours        £3.00 |
| 4 = All Day        £7.00 |
| 5 = All week       £14.00|
----------------------------
''')
In = input(">")
if In == "1":
    Payment = '1.20'

if In == "2":
    Payment = '2.40'

if In == "3":
    Payment = '3.00'

if In == "4":
    Payment = '7.00'

if In == "5":
    Payment = '14.00'
print(Payment)
print(f"Your current statement is £{Money}")
(Feb-27-2019, 04:39 PM)Vqlk Wrote: [ -> ]tried this but still not working help.
not working is not very descriptive. What exactly is the problem? It does not raise any exception, i.e. works as expected

Output:
---------------------------- |Press Enter To Continue | | | | | | | | | | Made by Ben| ---------------------------- ---------------------------- |How Long Are you Staying? | | 1 = 1 hour £1.20 | | 2 = 2 hours £2.40 | | 3 = 3 hours £3.00 | | 4 = All Day £7.00 | | 5 = All week £14.00| ---------------------------- >1 1.20 Your current statement is £50
It deos not work. Here is the code:
#Parking Ticket machine
Money = 50

import time
print('''
----------------------------
|Press Enter To Continue   |
|                          |
|                          |
|                          |
|                          |
|               Made by Ben|
----------------------------
''')
input("")
print('''
----------------------------
|How Long Are you Staying? |
| 1 = 1 hour         £1.20 |
| 2 = 2 hours        £2.40 |
| 3 = 3 hours        £3.00 |
| 4 = All Day        £7.00 |
| 5 = All week       £14.00|
----------------------------
''')
In = input(">")
if In == "1":
    Payment = '1.20'

if In == "2":
    Payment = '2.40'

if In == "3":
    Payment = '3.00'

if In == "4":
    Payment = '7.00'

if In == "5":
    Payment = '14.00'
print(Payment)
print(f"Your current statement is £{Money}")
and the error is :
Traceback (most recent call last):
File "C:/Users/bcola/Desktop/Other/python/app.py", line 41, in <module>
print(Payment)
NameError: name 'Payment' is not defined
it works and my output does show the result. I guess you didn't save the file after making the changes or are running different file