Trying to change a variable in use to what the user inputs - python

I have not found an answer that satisfies my needs or is simple enough for me to actually understand as I'm relatively new to python!
I have a variable labelled difficulty which asks the user to input a string to state whether they want easy medium or hard mode. Unfortunately I am unable to successfully have python check the input for the words used and give them what i want, i end up with "easy is not defined" or "medium is not defined" or "hard is not defined." Is there a way for me to get round this problem? Here is a small section of my code which the problem is enclosed to:
difficulty=input("What difficulty do you wish to choose, easy,medium or hard?")
if difficulty==easy:
print("You have chosen the easy mode, your test will now begin")
print("")
elif difficulty==medium:
print("You have chosen the medium mode, your test will now begin")
else:
print("You have chosen the hard mode or tried to be funny, your test
will now begin")

You are trying to get a string from the user (input will return a string) then compare it to another string in this case 'easy' or 'medium'. Here is a link to a google dev article talking about some of the basic things you can do to strings in python.
difficulty = input("What difficulty do you wish to choose, easy,medium or hard?")
if difficulty == 'easy':
print("You have chosen the easy mode, your test will now begin")
print("")
elif difficulty == 'medium':
print("You have chosen the medium mode, your test will now begin")
else:
print("You have chosen the hard mode or tried to be funny, your test will now begin")
When you put easy or medium in your code you are telling python that they are variables (link to python variable tutorial) not strings. In this case you have not defined the easy variable yet ie: easy = 'some data'. Because you have not defined it python does not know what to do with easy it will throw an error.

First of all, fix your indentation (if it is not a problem only in your example). Second, you need to put easy, medium, and hard in either single ' or double " quotes:
difficulty=input("What difficulty do you wish to choose, easy,medium or hard?")
if difficulty=="easy":
print("You have chosen the easy mode, your test will now begin")
print("")
elif difficulty=="medium":
print("You have chosen the medium mode, your test will now begin")
else:
print("You have chosen the hard mode or tried to be funny, your test
will now begin")
If you don't put them in quotes, then you aren't comparing difficulty to the word easy, but rather to the variable called easy. This of course causes an error, since no such variable exists. The quotes however tell python to interpret easy as a string.

Related

Checking if input matches variable with a list of strings

I am trying to make a calculator and the user must input which operation they want to use, I have only done addition so far but you get the point.
welcome = str(input("Welcome to the calculator, which form of operation do you wish to choose? "))
Addition = "Add", "add", "Addition", "addition", "+"
for x in Addition:
if welcome == Addition:
print("Works")
I run the code and when I input any of the strings in the "Addition" variable, it does not print "Works"?.
I have tried without the for loop and it still does not print "Works"
I am an absolute beginner at python and I just made this stackoverflow account today. I don't know how to write the code in proper code form in this question.
You are probably looking for in we can also use .lower() on the input so we have fewer things to match.
welcome = str(input("Welcome to the calculator, which form of operation do you wish to choose? ")).lower()
addition = ['add','addition','+']
if welcome in addition:
print('worked')

using while loop+if to make a simple game but input line not working

after learned while loop and if, i m trying to make a super simple text game, but here is the problem: after the input line asked to type right/left, and no matter i type left or right, the code wont work. i changed the "choice" variable to left then it's working properly... I dont know what's the problems here...please tell me how to fix this to make it work.
choice=str(input("you are now in a forest, please pick a direction(left or right)"))
while choice == "right":
print("a strange wind had sent you back to forest")
choice=str(input("you are now in a forest, please pick a direction(left or right)"))
if choice == "left":
print("u saw a cave, in or not?")
choice= str(input("type in or not "))
if choice == "in" :
print("u saw a bear claw and that's the last view in your eyes")
elif choice == "not":
print("after two and a half hours walking, you are now out of the forest")
that's the code, I think the input line got some problems but I don't know what exact problems are. when I change to choice="right" or choice="left", the code is working but with this input line it's not...
This code is working for me; are you using all lowercase left or right with no quotes or other characters? You may want to add a .lower().strip() to the end of your input call to lowercase everything and remove leading/trailing whitespace.
sorry guys I just found out why!
I have the habit to type a space between words which makes sentences clear but in this case, i type space+left/right and the while loop or if loop is looking for only "left' or "right" so I gave them a input they cannot recognize... that's stupid...
I am so sorry
thank you all for the help tho!

Encrypt already saved files on a computer

from string import maketrans
intab = "abcdefghijklmnopqrstuvwxyz"
outtab = "nopqrstuvwxyzabcdefghijklm"
trantab = maketrans(intab, outtab)
print "Do you want to translate a random term, or a file?(Please enter file name in the code)"
RandOrFile = raw_input ("Type 1 to enter a custom term, or 2 to translate a whole file")
if RandOrFile == "1":
inputA = raw_input ("Enter a phrase to translate")
str = inputA
print str.translate(trantab);
elif RandOrFile == "2":
(Code for my program)
I am attempting to create a program which encrypts a file, and turns the letters into other letters, like an Enigma Machine (But much more simple). I have made it so that you can directly translate user entered phrases, as the top chunk of the code shows, but I cant work out how to translate already made files on the users computer.
The code is meant to go under the "elif RandOrFile == "2"" line, but I cant work out how to a) fetch the data from a specified file on a computer, and b) work out how to translate that file in the program.
I have researched, but I cant find any ways to do so. My python skills are very beginner, so if you could possibly quote relevant sources to help on this, that would also be helpful, although I have tried, i'm not really sure where to begin while looking through tutorial websites.

Problems with Syntax and indentation errors

I'm going through a Book which, at this point in the book, requires me to make a small videogame that calls functions, uses if's, while's -- essentially all the things covered in the book so far. But, I get this error in this part of my code:
Code edited, get a new error.
File "ex35_study.py", line 24
third_scenario_code()
IndentationError: unindent does not match any outer indentation level
Here is my code:
options_thirdscenario_actions = ['Examine the door', 'Try to force it']
def third_scenario_code():
print "Let me try to crack this thing up, says Lars as he starts to type in the panel. You hear the sounds of the fight out there, there's not much time left. "
print "After receiving several commands a window with a code pop ups. "
print codefile.read()
def third_scenario():
print "You two get out of the cell and approach to the exit, a long corridor is ahead of you, flashing red lights indicate an state of emergency, you must evacuate."
print "As soon as you two approach to the door, it closes"
print "Crap it must be the emergency system, we have been detected"
next = raw_input("What do you do> ")
if next == 'Examine the door':
print "A small panel comes out, requires to enter a code of words"
third_scenario_code()
elif next == 'Try to force it':
print "You try to force the door with no result"
print options_thirdscenario_actions
next2 = raw_input("What else do you do> " )
if next2 = 'Examine the door'
third_scenario_code()
else:
print "You already did that"
I am getting a similar error on the whole program and I suspect it has something to do with indentation, but I have tried every suggestion I see in google with no fruitful result. thanks in advance.
You are missing colons after the one of if conditions and need to line things that are the same scope up, i.e. the print after the function call but you may also be mixing spaces and tabs. It is recommended to always use 4 spaces rather than tabs and most programming editors can be set up for this.
I would also suggest getting hold of pylint and using it. It will help you spot a lot of potential errors and will help you to develop good habits.
Its because of the indentation of third_scenario_code() you need to write it under the print .
change the following :
if next == 'Examine the door':
print "A small panel comes out, requires to enter a code of words"
third_scenario_code()
to :
if next == 'Examine the door':
print "A small panel comes out, requires to enter a code of words"
third_scenario_code()

Beginner: While loop not functioning properly, syntax errors, displays nothing

I am working through some Looping exercises, While statements in particular. Here are the instructions:
2.2) Modify the program so that it asks users whether they want to guess again each time. Use two variables, number for the number and answer for the answer to the question whether they want to continue guessing. The program stops if the user guesses the correct number or answers "no". (In other words, the program continues as long as a user has not answered "no" and has not guessed the correct number.)
Here is my code:
#!usr/bin/env python
#
#While statement
number = 24
while number != 24:
answer = raw_input("Guess my lucky number! Do you want to keep guessing?")
if number == 24:
print "You got it! That is great!"
elif answer == "no":
print "Thank you for playing."
else:
print "That is not the right answer! Try again."
When I run the module in IDLE, the end quote of That is great!" - becomes red and says invalid syntax. In terminal if I run $ python while.py nothing loads. I've tried writing this as Python 3 functions with print("") but it still does not run.
Thanks to anyone who can help with this.
The while-cycle is never entered because the condition number != 24 never holds.
This is why there is no output.
Your loop never executes because you state that number = 24, and then right after, your while loop will only start if number != 24.
In addition, raw_input will yield a string, not an int so either ask for "24" or cast the raw_input to an int.
It also seems that you don't actually give the user a chance to guess the number at all; you only ask the user if s/he wants to keep playing.
You might want to do something like this:
number = 24
answer = ""
while answer != str(number):
answer = raw_input("Guess my lucky number, or type 'no' to quit.")
if answer == "no":
print "Okay, see you later"
break
elif answer != str(number):
print "wrong number"
if answer == str(number):
print "you got it right"
Here's the syntax issues:
answer = ""
while answer != "24":
answer = raw_input("Guess my lucky number! Do you want to keep guessing?")
if answer == "24":
# You can fill in the rest ...
Well, I don't want to straight out solve it for you, but take a look at your conditional in the while loop. Think about what happens, line-by-line, when you run it, particularly in the "number" variable.
The other answers are touching on the problem but there are more...
Yes you really should be checking the answer variable in your loop instead of the number variable, but also keep in mind that raw_input is going to give you a string. So you will not get an int 24, you will get a string "24". The idea here is to take the answer variable from raw_input and check that variable against both your number and the value "no"
number = "24"

Categories