a= input("Password :" )
if a == "Donuts":
print "correct"
else:
print "wrong!"
This is the error in IDLE:
Traceback (most recent call last):
File "C:\Users\ME\Desktop\Python Files\test.py", line 1, in <module>
a= input("Password :" )
File "<string>", line 1, in <module>
NameError: name 'donuts' is not defined
I think you have to use single quotes around the word donuts instead of double quotes since those are used to define string literals, at least in C# and C++.
Rollback Post to RevisionRollBack
Quote from TheFieldZy »
Nobody's perfect, so neither is Hannah Montana Linux, but it's pretty great.
Quote from BC_Programming on Operating Systems »
They all suck. They just suck differently. Sort of like prostitutes.
I think you have to use single quotes around the word donuts instead of double quotes since those are used to define string literals, at least in C# and C++.
You can use single or double quotes in python, it doesn't really matter.
I think you have to use single quotes around the word donuts instead of double quotes since those are used to define string literals, at least in C# and C++.
I'm fairly certain he wants a string literal there.
My guess is that you're using Python 2.7, as that would work in Python 3. You are using the wrong input function; you want to use raw_input() to capture strings. input() captures integers or floats.
e. If you want to know more closely what is happening, then keep on reading.
When you call the input() function, it attempts to evaluate the name you've just given it. When you enter numbers, it just stores the numbers because they can't have names; however, if you try to enter a string, it assigns the name you to whatever the value is for the string you just gave input().
So let's say you have already defined a variable, and let's call it "foo". Now let's make another variable named "bar" that makes a call to the input() function. If we put in 'bar', instead of foo storing the string 'bar', it assigns the value of bar to foo. Some code to help explain:
>>> foo = 'string'
>>> bar = input()
foo
>>> foo
'string'
>>> bar
'string'
I was using python 2.7; before i was using 3.2 but not many tuturials out there for that..... Bass; i'm not quite following you at the moment. Is there somewhere i can go to learn this easier?
I was using python 2.7; before i was using 3.2 but not many tuturials out there for that..... Bass; i'm not quite following you at the moment. Is there somewhere i can go to learn this easier?
Yeah just for future reference, "raw_input" for a text input from the user in 2.7 is just "input" in 3.0+, and a few other things were changed around.
Well first of i'm adding something to my Original post instead of just making a new thread.
(I'm now using 3.2 because of some recomendtions i got btw)
everytime i run this it gives me the same error:
Code:
#Python 3.2.0
x='100'
buy1='20'
print ('You have, ' +x 'dollars')
print ('Would you like to buy an item for 20 dollars? : ')
confirm = input()
if confirm == ('yes'):
print ('You have' ,x-buy1, 'dollars left' )
else:
print ('you have', x, 'dollars left')
Error:
Gives me syntax errors:
Problem: I dont understand what i wrote wrong, can someone help me?
Well first of i'm adding something to my Original post instead of just making a new thread.
(I'm now using 3.2 because of some recomendtions i got btw)
everytime i run this it gives me the same error:
Code:
#Python 3.2.0
x='100'
buy1='20'
print ('You have, ' +x 'dollars')
print ('Would you like to buy an item for 20 dollars? : ')
confirm = input()
if confirm == ('yes'):
print ('You have' ,x-buy1, 'dollars left' )
else:
print ('you have', x, 'dollars left')
Error:
Gives me syntax errors:
Problem: I dont understand what i wrote wrong, can someone help me?
put parenthesis around the x-bu1 stuff and make sure to put "+" in between it and the strings. Do this for all of your print statements. That should be it. I can't be sure because you haven't actually given us the errors yet.
Well first of i'm adding something to my Original post instead of just making a new thread.
(I'm now using 3.2 because of some recomendtions i got btw)
everytime i run this it gives me the same error:
Code:
#Python 3.2.0
x='100'
buy1='20'
print ('You have, ' +x 'dollars')
print ('Would you like to buy an item for 20 dollars? : ')
confirm = input()
if confirm == ('yes'):
print ('You have' ,x-buy1, 'dollars left' )
else:
print ('you have', x, 'dollars left')
Error:
Gives me syntax errors:
Problem: I dont understand what i wrote wrong, can someone help me?
It would help if you posted where the issue happens exactly, but I see more than just a syntax error (use code tags!). I'm guessing the first syntax error occurs on this line:
print ('You have, ' +x 'dollars')
If you are going to do that, then you need to use the + sign on both sides of the x variable. However, the more Pythonic way of formatting your strings is to use, well, string formatting.
print ('You have %d dollars' % x)
That works if x is a number, else you'll want to use "%s" instead of "%d". Since you are trying to subtract x and buy1, they should both be numbers instead of strings.
First of all, it would probably be better to use one statement to set your x variable to itself minus buy1. The way you were doing it would display the proper amount, but the actual variable would not be changed.
Your actual error, though, was that you are not using the plus signs/commas correctly. Here's how your code could look:
x='100'
buy1='20'
print ('You have, ' + x + 'dollars')
print ('Would you like to buy an item for 20 dollars? : ')
confirm = input()
if confirm == ('yes'):
x = x - buy1
print ('You have' + x + 'dollars left' )
else:
print ('you have' + x + 'dollars left')
This won't actually work, though, because your x and buy1 variables are currently strings. If you want to use them as numbers instead, and do number-like operations on them, simply write the number without quotes. (This will make the variables integers). However, when you want to print out the variables to the screen, you will have to use str(var).
Another thing that you can do with this revised code is make it a little more compact. Here's the final code, you should see where I removed a few lines:
x=100
buy1=20
print ('You have, ' + str(x) + 'dollars')
print ('Would you like to buy an item for 20 dollars? : ')
confirm = input()
if confirm == ('yes'):
x = x - buy1
print ('you have'+ str(x) + 'dollars left')
I'm not going to fix your grammatical errors, though .
(Side note: you could also have the variables as strings, and when you are doing mathematical operations just use int(var) in the same way I used str(var) here. It just made more sense to have numbers saved as, well, numbers.)
To enhance the example more, I have more Pythonic code below.
x=100
buy1=20
print ('You have, %d dollars' % x)
confirm = input('Would you like to buy an item for 20 dollars? : ')
if confirm == 'yes':
x -= buy1
print ('You have %d dollars left' % x)
It accomplishes the same task, but it does so in less code.
Rollback Post to RevisionRollBack
Windows 7 | Core i5 2500k | Patriot 8GB | HD 6870 | Capstone 450 | Z68ITX-A-E | Barracuda LP 2TB | Chronos 120GB | Lian Li PC-Q08B
Ultrasharp U2211H | Klipsch ProMedia 2.1 Klipsch S4 | RAT 9 | Das Keyboard
a= input("Password :" )
if a == "Donuts":
print "correct"
else:
print "wrong!"
This is the error in IDLE:
Traceback (most recent call last):
File "C:\Users\ME\Desktop\Python Files\test.py", line 1, in <module>
a= input("Password :" )
File "<string>", line 1, in <module>
NameError: name 'donuts' is not defined
Can someone tell me what is wrong? Thanks!
You can use single or double quotes in python, it doesn't really matter.
I'm fairly certain he wants a string literal there.
Okay. I've never used Python so I wasn't really sure how the syntax works with it.
e. If you want to know more closely what is happening, then keep on reading.
When you call the input() function, it attempts to evaluate the name you've just given it. When you enter numbers, it just stores the numbers because they can't have names; however, if you try to enter a string, it assigns the name you to whatever the value is for the string you just gave input().
So let's say you have already defined a variable, and let's call it "foo". Now let's make another variable named "bar" that makes a call to the input() function. If we put in 'bar', instead of foo storing the string 'bar', it assigns the value of bar to foo. Some code to help explain:
Ultrasharp U2211H |
Klipsch ProMedia 2.1Klipsch S4 | RAT 9 | Das Keyboard(I'm now using 3.2 because of some recomendtions i got btw)
everytime i run this it gives me the same error:
Code:
#Python 3.2.0
x='100'
buy1='20'
print ('You have, ' +x 'dollars')
print ('Would you like to buy an item for 20 dollars? : ')
confirm = input()
if confirm == ('yes'):
print ('You have' ,x-buy1, 'dollars left' )
else:
print ('you have', x, 'dollars left')
Error:
Gives me syntax errors:
Problem: I dont understand what i wrote wrong, can someone help me?
if a == "Donuts":
print "correct"
else:
print "wrong!"
that should work, if youre using python 2.7 if not try:
a= str(raw_input("Password :" ))
if a == "Donuts":
print "correct"
else:
print "wrong!"
I'm asking about the second Piece of code i stated in my comment earlier today. And i am using python 3.2.
put parenthesis around the x-bu1 stuff and make sure to put "+" in between it and the strings. Do this for all of your print statements. That should be it. I can't be sure because you haven't actually given us the errors yet.
It would help if you posted where the issue happens exactly, but I see more than just a syntax error (use code tags!). I'm guessing the first syntax error occurs on this line:
If you are going to do that, then you need to use the + sign on both sides of the x variable. However, the more Pythonic way of formatting your strings is to use, well, string formatting.
That works if x is a number, else you'll want to use "%s" instead of "%d". Since you are trying to subtract x and buy1, they should both be numbers instead of strings.
To enhance the example more, I have more Pythonic code below.
It accomplishes the same task, but it does so in less code.
Ultrasharp U2211H |
Klipsch ProMedia 2.1Klipsch S4 | RAT 9 | Das Keyboard