PDA

View Full Version : Python Code error



Cha0tic
04-23-2014, 07:46 PM
Hey guys trying to do my homework assignment but having a issue when running the code
Code error
Traceback (most recent call last):
File "C:/Users/Me/Desktop/hotel.py", line 68, in <module>
print ("Total Bill : ",total)
NameError: name 'total' is not defined

Its telling me total isn't defined but I am defined it within code, here it is.
My code is just probably off and terrible.

def main():
# input section
name = input("Enter The Patients Name Please : ")
days = int(input("Enter the Number of Days in the Hotel : "))
print("Room Used, \n",
"\t", "1 - Single room - One Bed\n"
"\t", "2 - Family room - Double Bed\n"
"\t", "3 - Suite")
room = int(input("Enter Choice 1, 2 or 3 : "))

internet = input("Would you like access to the internet(y/n) : ")
if internet == 'y':
print("\t", "Internet Access Useage\n",
"1 - Wirless\n",
"2 - Wired")
access = int(input("Enter Choice 1 or 2 : "))
tv = input("Would you like to use the TV(y/n) : ")
if tv == 'y':
print("\t", "TV Usage\n"
"1 - Cable\n"
"2 - Basic Channels")
tvchoice = int(input("Enter Choice 1 or 2 :"))



# processing section
if room == 1:
roomprice = '225'
elif room == 2:
roomprice = '325'
elif room == 3:
roomprice = '550'

if access == 1:
accessprice == '9.95'
elif access == 2:
accessprice == '5.95'

if tv == 1:
tvprice == '9.95'
elif access == 2:
tvprice == '5.95'

total = roomprice+accessprice+tvprice









#output section

print ("Total Bill : ",total)


main()

whiterice77
04-23-2014, 07:49 PM
move it back 1 tab, python uses indentation to enclose chit in loops and its stuck in the last elif even with the empty line

Cha0tic
04-23-2014, 07:51 PM
move it back 1 tab, python uses indentation to enclose chit in loops and its stuck in the last elif even with the empty line

Thanks brah did just that now its telling my roomprice isn't defined.

whiterice77
04-23-2014, 07:53 PM
try changing the last elif to an else statement, the way it is now if roomprice isn't one of those 3 options it'll be undefined as you never initialized it outside of the loops. Another option is to initialize all the vars early on and set their values later, give them placeholders like -1 or something.

edit: that might not work, can't remember if python will let you define a var in if else statments like that. initialize vars should work tho

Cha0tic
04-23-2014, 08:08 PM
try changing the last elif to an else statement, the way it is now if roomprice isn't one of those 3 options it'll be undefined as you never initialized it outside of the loops. Another option is to initialize all the vars early on and set their values later, give them placeholders like -1 or something.

edit: that might not work, can't remember if python will let you define a var in if else statments like that. initialize vars should work tho

Kind of frustrated since this isn't in our book, changed the last elif statement to a else.
This is the code now
if room == 1:
roomprice = 225
elif room == 2:
roomprice = 325
else:
roomprice = 550

if access == 1:
accessprice == '9.95'
else:
accessprice == '5.95'

if tv == 1:
tvprice == '9.95'
else:
tvprice == '5.95'
total = roomprice+accessprice+tvprice


Still getting the same error, roomprice is not defined.

FrazettaSilke
04-23-2014, 08:20 PM
You have to declare "room price" first, as well as the others
Before your if statements, you need to declare roomcount=0, in order for Python to read it in.
i.e.

roomcount = 0
tvprice = 0
accessprice = 0
if room == 1:
roomprice = 225
elif room == 2:
roomprice = 325
else:
roomprice = 550

if access == 1:
accessprice == '9.95'
else:
accessprice == '5.95'

if tv == 1:
tvprice == '9.95'
else:
tvprice == '5.95'

ziaziazia
04-23-2014, 08:25 PM
roomprice = 350
#bout 350

if room == 1:
roomprice = 225
elif room == 2:
roomprice = 325
else:
roomprice = 550

whiterice77
04-23-2014, 08:26 PM
You have to declare "room price" first, as well as the others
Before your if statements, you need to declare roomcount=0, in order for Python to read it in.
i.e.

roomcount = 0
tvprice = 0
accessprice = 0
if room == 1:
roomprice = 225
elif room == 2:
roomprice = 325
else:
roomprice = 550

if access == 1:
accessprice == '9.95'
else:
accessprice == '5.95'

if tv == 1:
tvprice == '9.95'
else:
tvprice == '5.95'

this is what I was trying to say in the edit, you might also want to check what you're doing when setting access and tvprice.. I think you're trying to put accessprice = 9.95 not sure if you looked at those lines yet tho.

I'd probably do a quick check on the input before setting the prices as well to make sure they aren't entering nonsense and getting a real answer back

FrazettaSilke
04-23-2014, 08:28 PM
this is what I was trying to say in the edit, you might also want to check what you're doing when setting access and tvprice.. I think you're trying to put accessprice = 9.95 not sure if you looked at those lines yet tho.

I'd probably do a quick check on the input before setting the prices as well to make sure they aren't entering nonsense and getting a real answer back

CS master racing checking in. rep on site for CS

Cha0tic
04-23-2014, 08:58 PM
Might just be me, but I did that in my current code
roomprice = 0
if room == 1:
roomprice = 225
elif room == 2:
roomprice = 325
else:
roomprice = 550

and at the end I still get roomprice is not defined.

FrazettaSilke
04-23-2014, 09:00 PM
Might just be me, but I did that in my current code
roomprice = 0
if room == 1:
roomprice = 225
elif room == 2:
roomprice = 325
else:
roomprice = 550

and at the end I still get roomprice is not defined.
Did you dedent just the "Total" or the whole file? If you just dedented "Total," you still need to do the same for the rest of the code.

Cha0tic
04-23-2014, 09:01 PM
Did you dedent just the "Total" or the whole file? If you just dedented "Total," you still need to do the same for the rest of the code.

Apologize if this a dumb question, dedent total? What do you mean by that?

FrazettaSilke
04-23-2014, 09:04 PM
Apologize if this a dumb question, dedent total? What do you mean by that?
The variable "Total."
total = roomprice+accessprice+tvprice

Did you just backspace that line of code, or the whole file? Sounds like the whole file needs to be "dedented."

FrazettaSilke
04-23-2014, 09:07 PM
http://i59.tinypic.com/zkol7q.png

Cha0tic
04-23-2014, 09:08 PM
The variable "Total."
total = roomprice+accessprice+tvprice

Did you just backspace that line of code, or the whole file? Sounds like the whole file needs to be "dedented."

Oh, yes I just back spaced it.
Dedenting it just fixed my issue, this is why I have a love/hate relationship with coding.
Thanks alot time to continue on with this assignment

Cha0tic
04-23-2014, 09:17 PM
For some reason whever it totals now, it stops at roomprice. It doesn't add the access price or tvprice even though they are defined.

FrazettaSilke
04-23-2014, 09:23 PM
For some reason whever it totals now, it stops at roomprice. It doesn't add the access price or tvprice even though they are defined.
Fixed :)

http://i61.tinypic.com/111ph8i.png

You didn't have the function "Main" indented correctly, and you needed to declare the variables before calling them. Also, you can't convert between "int" and "str." You have to explicitly declare strings if you want to print the variables the way you want. You might want to add functionality when the user returns "n" for TV/NET, I broke the code doing that. PM for the file

knave4567
04-23-2014, 09:25 PM
def main():
# input section
name = raw_input("Enter The Patients Name Please : ")
days = int(raw_input("Enter the Number of Days in the Hotel : "))
print("Room Used, \n","\t", "1 - Single room - One Bed\n""\t", "2 - Family room - Double Bed\n""\t", "3 - Suite")
room = int(raw_input("Enter Choice 1, 2 or 3 : "))
if room == 1:
roomprice = 225
elif room == 2:
roomprice = 325
elif room == 3:
roomprice = 550
internet = raw_input("Would you like access to the internet(y/n) : ")
if internet == 'y':
print("\t", "Internet Access Useage\n","1 - Wirless\n","2 - Wired")
access = int(raw_input("Enter Choice 1 or 2 : "))
if access == 1:
accessprice = 9.95
elif access == 2:
accessprice = 5.95
else:
accessprice = 0
tv = raw_input("Would you like to use the TV(y/n) : ")
if tv == 'y':
print("\t", "TV Usage\n""1 - Cable\n""2 - Basic Channels")
tvchoice = int(raw_input("Enter Choice 1 or 2 :"))
if tv == 1:
tvprice = 9.95
elif tv == 2:
tvprice = 5.95
else:
tvprice = 0

total = roomprice + accessprice + tvprice
print (total)

main()


Your code is really really really really bad. The code above should work, study what i did or else you're ****ed if something like this going to be in a test/exam

Cha0tic
04-23-2014, 09:26 PM
Fixed :)

http://i61.tinypic.com/111ph8i.png

You didn't have the function "Main" indented correctly, and you needed to declare the variables before calling them. Also, you can't convert between "int" and "str." You have to explicitly declare strings if you want to print the variables the way you want. You might want to add functionality when the user returns "n" for TV/NET, I broke the code doing that. PM for the file

Thanks alot, and also pm sent :)
Also I didn't mean to use ' for some of my numbers I forgot to remove them as I was trying to see why I was getting errors.

Cha0tic
04-23-2014, 09:29 PM
def main():
# input section
name = raw_input("Enter The Patients Name Please : ")
days = int(raw_input("Enter the Number of Days in the Hotel : "))
print("Room Used, \n","\t", "1 - Single room - One Bed\n""\t", "2 - Family room - Double Bed\n""\t", "3 - Suite")
room = int(raw_input("Enter Choice 1, 2 or 3 : "))
if room == 1:
roomprice = 225
elif room == 2:
roomprice = 325
elif room == 3:
roomprice = 550
internet = raw_input("Would you like access to the internet(y/n) : ")
if internet == 'y':
print("\t", "Internet Access Useage\n","1 - Wirless\n","2 - Wired")
access = int(raw_input("Enter Choice 1 or 2 : "))
if access == 1:
accessprice = 9.95
elif access == 2:
accessprice = 5.95
else:
accessprice = 0
tv = raw_input("Would you like to use the TV(y/n) : ")
if tv == 'y':
print("\t", "TV Usage\n""1 - Cable\n""2 - Basic Channels")
tvchoice = int(raw_input("Enter Choice 1 or 2 :"))
if tv == 1:
tvprice = 9.95
elif tv == 2:
tvprice = 5.95
else:
tvprice = 0

total = roomprice + accessprice + tvprice
print (total)

main()


Your code is really really really really bad. The code above should work, study what i did or else you're ****ed if something like this going to be in a test/exam

Edit: You're gonna have to indent this stuffs cuz for some reason Bodybuilding doesnt indent ****

Honestly we didn't even go over using raw_input, he never even mentioned it. This is literally how code looks in the book and in class, maybe a bit cleaner but the same.

spade22
04-23-2014, 09:32 PM
Honestly we didn't even go over using raw_input, he never even mentioned it. This is literally how code looks in the book and in class, maybe a bit cleaner but the same.

Look at what you are doing here:

if room == 1:
roomprice = '225'
elif room == 2:
roomprice = '325'
elif room == 3:
roomprice = '550'

if access == 1:
accessprice == '9.95'
elif access == 2:
accessprice == '5.95'

if tv == 1:
tvprice == '9.95'
elif access == 2:
tvprice == '5.95'


the error is here, look at accessprice and tvprice

spade22
04-23-2014, 09:33 PM
Remember you want to SET the variables....

Cha0tic
04-23-2014, 09:35 PM
Look at what you are doing here:

if room == 1:
roomprice = '225'
elif room == 2:
roomprice = '325'
elif room == 3:
roomprice = '550'

if access == 1:
accessprice == '9.95'
elif access == 2:
accessprice == '5.95'

if tv == 1:
tvprice == '9.95'
elif access == 2:
tvprice == '5.95'


the error is here, look at accessprice and tvprice

I've changed that, that was a error on my end.

See, current code.
def main():
# input section
name = input("Enter The Patients Name Please : ")
name = input("Enter The Patients Name Please : ")
days = int(input("Enter the Number of Days in the Hotel : "))
print("Room Used, \n",
"\t", "1 - Single room - One Bed\n"
"\t", "2 - Family room - Double Bed\n"
"\t", "3 - Suite")
room = int(input("Enter Choice 1, 2 or 3 : "))

internet = input("Would you like access to the internet(y/n) : ")
if internet == 'y':
print("\t", "Internet Access Useage\n",
"1 - Wirless\n",
"2 - Wired")
access = int(input("Enter Choice 1 or 2 : "))
tv = input("Would you like to use the TV(y/n) : ")
if tv == 'y':
print("\t", "TV Usage\n"
"1 - Cable\n"
"2 - Basic Channels")
tvchoice = int(input("Enter Choice 1 or 2 :"))





# processing section
roomprice = 0
accessprice = 0
tvprice = 0

if room == 1:
roomprice = 225
elif room == 2:
roomprice = 325
elif room == 3:
roomprice = 550

if access == 1:
accessprice == 9.95
elif access == 2:
accessprice == 5.95

if tv == 1:
tvprice == 9.95
else:
tvprice == 5.95
total = (roomprice) + (accessprice) + (tvprice)







#output section

print ("Total Bill : ",total)


main()

spade22
04-23-2014, 09:37 PM
There was no need to change the original code or to set those variables

The error is in the part I showed you, which you still have in this new code you showed.

knave4567
04-23-2014, 09:38 PM
Honestly we didn't even go over using raw_input, he never even mentioned it. This is literally how code looks in the book and in class, maybe a bit cleaner but the same.

There's no difference really. raw_input is just much more safer, u can change it to input and it will still work.

Also, i've edited the code to have indenting applied. Scroll up

spade22
04-23-2014, 09:40 PM
I've marked up your code read it.
# processing section
if room == 1:
roomprice = 225
elif room == 2:
roomprice = 325
elif room == 3:
roomprice = 550

if access == 1:
#ERROR HERE accessprice == 9.95
elif access == 2:
#ERROR HERE accessprice == 5.95

if tv == 1:
#ERROR HERE tvprice == 9.95
else:
#ERROR HERE tvprice == 5.95

#THERE WILL BE AN ERROR HERE WHEN YOU WANT TO PRINT total = (roomprice) + (accessprice) + (tvprice)

Cha0tic
04-23-2014, 09:44 PM
I've marked up your code read it.
# processing section
if room == 1:
roomprice = 225
elif room == 2:
roomprice = 325
elif room == 3:
roomprice = 550

if access == 1:
#ERROR HERE accessprice == 9.95
elif access == 2:
#ERROR HERE accessprice == 5.95

if tv == 1:
#ERROR HERE tvprice == 9.95
else:
#ERROR HERE tvprice == 5.95

#THERE WILL BE AN ERROR HERE WHEN YOU WANT TO PRINT total = (roomprice) + (accessprice) + (tvprice)

Why exactly is there a error in those parts when roomprice is done the same way?

spade22
04-23-2014, 09:46 PM
Why exactly is there a error in those parts when roomprice is done the same way?

You are not doing roomprice the same way. What you want to do is set accessprice and tvprice to a certain float. You are not doing that. If you are completely lost let me know I will just give you the answer.

knave4567
04-23-2014, 09:47 PM
Why exactly is there a error in those parts when roomprice is done the same way?

your using "==" which means "equals". YOu should be using "=", which means "assign as"

Cha0tic
04-23-2014, 09:54 PM
your using "==" which means "equals". YOu should be using "=", which means "assign as"
Wow, I didn't even notice that till you pointed it out.
holy chit I feel retarded now

spade22
04-23-2014, 09:57 PM
Wow, I didn't even notice that till you pointed it out.
holy chit I feel retarded now

There is still a big error. Run the program and select N for both tv and internet

Cha0tic
04-23-2014, 10:00 PM
There is still a big error. Run the program and select N for both tv and internet

Hm I see, didn't add if statements for no.
Also with the fix code, my final statement for tvprice
else:
tvprice = 5.95, no matter if I enter 1 or 2, it always adds 5.95 even though 1 is 9.95

spade22
04-23-2014, 10:04 PM
Hm I see, didn't add if statements for no.
Also with the fix code, my final statement for tvprice
else:
tvprice = 5.95, no matter if I enter 1 or 2, it always adds 5.95 even though 1 is 9.95

You want to check if tvchoice is 1 not if tv is 1:

also you should only set these variables IF they select yes for the question:

if internet == 'y' and access == 1:
accessprice = 9.95
elif internet == 'y' and access == 2:
accessprice = 5.95
else:
accessprice = 0

if tv == 'y' and tvchoice == 1:
tvprice = 9.95
elif tv == 'y' and tvchoice == 2:
tvprice = 5.95
else:
tvprice = 0


The last else will be for the cases when the user does not select 'y'

Cha0tic
04-23-2014, 10:16 PM
You want to check if tvchoice is 1 not if tv is 1:

also you should only set these variables IF they select yes for the question:

if internet == 'y' and access == 1:
accessprice = 9.95
elif internet == 'y' and access == 2:
accessprice = 5.95
else:
accessprice = 0

if tv == 'y' and tvchoice == 1:
tvprice = 9.95
elif tv == 'y' and tvchoice == 2:
tvprice = 5.95
else:
tvprice = 0


The last else will be for the cases when the user does not select 'y'

Thanks again, I was just working on that. Code is running as it should for now, still not completed.

spade22
04-23-2014, 10:21 PM
No problem, let me know if you need any other help.

Cha0tic
04-23-2014, 10:28 PM
No problem, let me know if you need any other help.

I may need help in about 30 minutes or so, once I get to part2. I basically have to modify my code adding loops.
Mind if I just pm you the hw objective and requirements so you understand better what exactly I was told to do?

spade22
04-23-2014, 10:30 PM
Ya that's fine.

Cha0tic
04-23-2014, 10:53 PM
pm sent :)

Huntsmana
04-24-2014, 05:20 AM
OP you seem to be around my level in python, maybe a few weeks ahead of my course.

do you know how i can do all the stuff you're doing but in ruby or java? like bringing up the actual shell and then creating another file which will run to the shell. i have no idea what the fuk im doing with notepad++ or netbeans and just want to be able to use ruby and java the same or similar way to python.

want to know because i want to learn ruby and java by myself by doing the same projects im doing in python

Huntsmana
04-24-2014, 05:30 AM
pls respond.

FrazettaSilke
04-24-2014, 06:12 AM
We are not your professors, why don't you simply email your teachers? All the teachers I've known were more than willing to help.

Huntsmana
04-24-2014, 06:14 AM
We are not your professors, why don't you simply email your teachers? All the teachers I've known were more than willing to help.

chur

FrazettaSilke
04-24-2014, 06:16 AM
chur
http://www.thegeekstuff.com/2009/10/ruby-hello-world-example-how-to-write-and-execute-ruby-program-on-unix-os/

you from new zealand brah?