NameError: name 'Right' is not defined - python-2.7

I'm trying to execute the following code in Spyder 3.3.1 using Python 2.7.15. I'm a beginner.
text = str(input("You are lost in forest..."))
while text == "Right":
text = str(input("You are lost in forest..."))
print "You got out of the forest!!!"
When I run the code with integer value it works. For example the following piece of code:
text = input("You are lost in forest...")
while text == 1:
text = input("You are lost in forest...")
print "You got out of the forest!!!"
How can I make the input() work with a string value? Thank you for your help.

Use raw_input() instead of input():
value = raw_input("You are lost in forest...") # String input
value = int(raw_input("You are lost in forest...")) # Integer input
...

In python 2, raw_input() takes exactly what the user typed and passes it back as a string. input() tries to understand the data entered by the user.Hence expects a syntactically correct python statement.That's why you got an error when you enter Right. So you can enter "Right" as input to fix this error.
But it is better to use raw_input() instead of input().

Related

python hangman code for a beginner

I just started to learn python about a week ago. I tried to create a simple hangman game today. All of my code in this works so far, but there is one thing that I cannot think of how to implement. I want the code to print 'you win' when it the player correctly types 'python', letter by letter. But I cant seem to end it after they get it right. It will end if they type 'python' in one attempt, opposed to letter form. My attempt to do it is on the line with the .join. I can't seem to figure it out though. Any help or advice for a new programmer would be greatly appreciated.
guesses = []
count = 1
ans = 'python'
word = ''
while count < 10:
guess = raw_input('guess a letter: ')
guesses.append(guess)
if ''.join(word) == ans:
print 'you win'
break
elif len(guess) > 1 and ans == guess:
print ans
print 'you win'
break
else:
for char in ans:
if char in guesses:
word.append(char)
print char,
else:
print '_',
count += 1
else:
print '\nyou lose'
First, I want to start off by saying, unless you are dealing with legacy code or some library which you need that only works in 2.7, do not use python 2.7, instead use python 3.x (currently on 3.6). This is because soon 2.7 will be deprecated, and 3.6 + has a lot more features and a lot of QOL improvements to the syntax and language you will appreciate (and has support for functionality that 2.7 just doesn't have now).
With that said, I'll make the translation to 3.6 for you. it barely makes a difference.
guesses = []
count = 1
ans = 'python'
word = ''
while count < 10:
guess = input('guess a letter: ')
guesses.append(guess)
if ''.join(word) == ans:
print('you win')
break
elif len(guess) > 1 and ans == guess:
print(ans)
print('you win')
break
else:
for char in ans:
if char in guesses:
word.append(char)
print(char)
else:
print('_')
count += 1
else:
print('\nyou lose')
The only two changes here are that print now requires parenthesis, so every print 'stuff' is now print('stuff'), and raw_input is now input('input prompt'). Other than that, I'm suprised you were able to get away with word.append(char). You cannot use append() on a python str in either 2.7 or 3.x. I think you were trying to use it as an array, as that is the only reason you would use ''.join(word). To fix this I would do word = [] instead of word = ''. now your ''.join(word) should work properly.
I would advise you to take the next step and try to implement the following things to your program: If the user doesn't enter a single character, make it so that the characters are not added to the guesses list. Try making this a main.py file if you haven't already. Make parts of the program into functions. Add a new game command. Add an actual hangman in chars print out every time. Add file io to read guess words (ie instead of just python, you could add a lot of words inside of a file to chose).

Can someone tell me whats wrong with this python code?

Note: I'm using Python 2.7
I'm not very experienced at Python, but I decided to make a small simple program. Here is the code:
import random
while True:
randomNumber = random.randrange(1, 3)
print randomNumber
guessedNumber = raw_input("Choose a number between 1 and 3 ")
if randomNumber == guessedNumber:
print 'Yay! You got it right!'
else:
print 'You got it wrong :( The number was:',randomNumber
#The first print is just for testing.
But when I try to run it I get this:
IDLE after i used the program a few times
Can someone tell me what i need to change or what is wrong with the code?
raw_input returns a string to guessedNumber, and your program compares a string (guessedNumber) to an integer (randomNumber), so if randomNumber == guessedNumber never evaluates to True.
The solution is to convert guessedNumber to an int and then compare the two values.

Automatic conversion from file to string after entering in a for loop?

v_file = open('numbers.txt','r')
print (type(v_file))
for v_i in v_file:
print (v_i.strip('\n'))
print (type(v_i))
Hey there... i'm just wondering how python knows to change automatically from a file type to a string type in this piece of code after entering the for loop.
In "numbers.txt" i have let's say:
Peter, 0908212
Joe, 9283812
L.T: It just knows and that is it?
I'm a bit unclear on what you are trying to accomplish, but I'm gonna assume those numbers are in the file. That being said, try:
content = v_file.read()
for line in content.split('\n'):
print line
## ... or whatever. Should return those numbers
Again, I'm assuming you are just iterating over an open file instance.
Hope that helps!

write a one year calendar to file using python

When I run the below code it prints out a calendar for an entire year (which I don't want). I want it to write to file but it won't. It also returns the error message TypeError: expected a character buffer object. Also, casting it into a string doesn't work.
import calendar
cal = calendar.prcal(2015)
with open('yr2015.txt', 'w') as wf:
wf.write(cal)
As an example, the below code prints one month of a year and returns a string, but this isn't what I want
print calendar.month(2015, 4)
print type(calendar.month(2015, 4))
So when I run the below code I get the error message <type 'NoneType'>. It seems to me that it should be a string but obviously isn't. Any suggestions on how I can get a 12-month calendar into a text file?
print type(calendar.prcal(2015))
prcal doesn't return anything. Use cal = calendar.TextCalendar().formatyear(2015) Instead.
Your question is a bit confusing. However, you don't have to make python write to the file.
Let python write to stdout and redirect stdout to a file
python myCal.py > yr2015.txt
This should do the trick
That is because calendar.prcal() will only print the calendar of an year. And it wont return you any values. So this line in your code print type(calendar.prcal(2015)) will return none type error.

Print function in python error

I have just started learning python 2.7, and as every new learner i am still getting accustomed to the syntax that python uses. I tried to write this code:
name = raw_input('What is your name?\n')
print 'Hi, %s.' % (name)
I guess the output for the above program should be:
Hi, What is your name?
But i am getting the output as:
What is your name?
After pressing enter key, i get another output:
Hi, .
What is the problem with my code?
There is no problem, it does exactly what you tell it to.
raw_input prints the string argument (as a prompt), and reads input from the user until the first newline, returning the text it has read. This is exactly what happens (try typing some text before hitting Enter). Then the second line takes that text and puts it into the formatting string, printing the result.
The raw_input function is used to take input form the prompt. The string you passed in function will be printed on the prompt followed by your input which gets completed (in sense of python) with the pressing of 'enter key'.
After that the next line print 'Hi, %s.' % (name) will get printed showing the name entered by user in the prompt, which, I assume, is None in your case as you are pressing enter key without any input.