I a new in programming , was trying some concepts with if else in python
The if else statement is not working as it should.
I'm using nested if-else , however only included the basic code in the code block
I am using a string as an input and then comparing the input with if else statements.
I tried the code in Thonny ide and it works when I debug the program , but after trying to run the program it does not print anything . Alternatively if I use an else statement instead of the elif in the end , only the code in the else statement will print
the code is :
new_value = input("enter your choice between left and right")
if new_value =='left':
print("You chose left")
elif new_value =="right":
print("you chose right")
This code is correct.
new_value = input("enter your choice between left and right")
if new_value =="left":
print("You chose left")
elif new_value =="right":
print("you chose right")
Alternatively if you use an else statement reffer it,
if new_value =='left':
print("You chose left")
else:
print("you chose right")
provide your full nested loop so i will understand problem.
Related
I have the following code.
while True:
# Prompt
command = input("> ").upper()
if command == "WEST" or "IN":
if adventure.move(command) == True:
print("True")
else:
print("You cannot go there")
elif command == "QUIT":
print("Thanks for playing!")
exit()
else:
print("Invalid command")
The idea is to prompt the user for a command. If the command is either direction "WEST" or "IN" its supposed to move and give a description. This all works. The idea is that an adventure consists of several rooms a user must navigate through
For the record: adventure.move(command) returns True if the move was succesful, and False if the move could not be made. Because there was no direction to be going in, for example.
The problem is that if I give a command like QUIT or FOO I am expecting a different result. However, this does not happen.
>WEST
True (move successful)
>QUIT
You cannot go there
>FOO
You cannot go there
It seems that whatever I type; it will always accept the first if statement.
Any clue what I am doing wrong?
You need to change your first if statement to this
while True:
# Prompt
command = input("> ").upper()
if command == "WEST" or command == "IN":
if adventure.move(command) == True:
print("True")
else:
print("You cannot go there")
elif command == "QUIT":
print("Thanks for playing!")
exit()
else:
print("Invalid command")
if command == "WEST" or "IN": will always evalutate to true because it is actually asking if command is equal to west or the equivalent of bool("IN") which it will always return true unless it is an empty string. And in an or statement if either of the logic tests return true then the code will be executed in the if block.
I'm just trying to write a code for myself and I have problem with calling a specific function in my code and it is weird because I already have 2 more functions just like this one and they do their job correctly check it out
import random
name = ("aghayan","jafari","panahi","kashkool")
word = random.choice(names)
dash_guess = "-" * len(word)
guesses_left = 5
class hangman():
def Entrance():
print(" one of your python classmates was an undercover cop and set a ")
print(" trap for our Boss Mohammad Esmaili!'THE CARTEL KING' so they arrest him .")
print(" we just need that snitch name and your the only person of that")
print(" class that we have access to , so your going to tell us the snitch")
print(" name or i will hang you my self and you got only 5 chances to ")
print(" tell me his or hers name or you'll die")
print()
def repeat():
your_choice =input(" so will you help me or you want to die ? 'yes' or 'no' : ")
if your_choice == "yes":
print("Good it seems you have someone waiting for you and you want to ")
print("see him/her again , you better be telling the truth or i,ll go ")
print("and pay a visit to your love")
core_game(guess)
elif your_choice == "no":
print("ok good choice , it will be my pleasure to kill you ")
print("________ ")
print("| | ")
print("| 0 ")
print("| /|\ ")
print("| / \ ")
print("| ")
print("Adios my friend , i hope you rest in peace in HELL")
exit()
else :
print(" it seems the noose tightens around your neck and its getting")
print(" hard to talk but i just need 'yes' or 'no' for answer")
repeat()
repeat()
Entrance()
def core_game(guess):
while guesses_left > 0 and not dash_guess == word:
guess = input("so tell me that snitch name letter by letter : ")
if guess != 1:
print("NOPE , i need you to spell the name of that rat")
core_game(guess)
game = hangman()
It's not complete but the question is when I enter 'yes' it should take the program to def core_game() but it give me error that " core_game is not defined ".
This section is your problem:
def core_game(guess):
while guesses_left > 0 and not dash_guess == word:
guess = input("so tell me that snitch name letter by letter : ")
if guess != 1:
print("NOPE , i need you to spell the name of that rat")
core_game(guess)
The lack of indent on the last line drops you out of the class definition. In other words, you're calling core_game from the global scope (where it's not defined) rather than from within the class (where it is defined).
Python is picky with indenting and formatting; I'd advise you to take some time to learn how to correctly format your code for Python, which will not only help you reduce errors but will also make your code significantly easier for you and anyone else to read.
Your solution is to remove the core_game(guess) call entirely. You don't need it, because you're already calling Entrance(), and that calls core_game at the correct points for you.
You've also got another issue - your core_game method has a guess parameter, but it's not necessary and it's making it hard for you to call it correctly:
def core_game(guess):
while guesses_left > 0 and not dash_guess == word:
# On this line, you're overwriting the value of the guess parameter
# before you've actually read it. Hence, you don't actually
# need that parameter at all.
guess = input("so tell me that snitch name letter by letter : ")
if guess != 1:
print("NOPE , i need you to spell the name of that rat")
And, where you call it:
if your_choice == "yes":
print("Good it seems you have someone waiting for you and you want to ")
print("see him/her again , you better be telling the truth or i,ll go ")
print("and pay a visit to your love")
# At this point, guess is not defined, so you're not passing anything
# to the core_game function.
core_game(guess)
Given that (a) you're not passing anything, and (b) you never actually use the parameter, you can just remove it.
After all the suggestions above, here's how your code looks:
import random
name = ("aghayan", "jafari", "panahi", "kashkool")
word = random.choice(names)
dash_guess = "-" * len(word)
guesses_left = 5
class Hangman():
def entrance(self):
print(" one of your python classmates was an undercover cop and set a ")
print(" trap for our Boss Mohammad Esmaili!'THE CARTEL KING' so they arrest him .")
print(" we just need that snitch name and your the only person of that")
print(" class that we have access to , so your going to tell us the snitch")
print(" name or i will hang you my self and you got only 5 chances to ")
print(" tell me his or hers name or you'll die")
print()
def repeat():
your_choice =input(" so will you help me or you want to die ? 'yes' or 'no' : ")
if your_choice == "yes":
print("Good it seems you have someone waiting for you and you want to ")
print("see him/her again , you better be telling the truth or i,ll go ")
print("and pay a visit to your love")
core_game(guess)
elif your_choice == "no":
print("ok good choice , it will be my pleasure to kill you ")
print("________ ")
print("| | ")
print("| 0 ")
print("| /|\ ")
print("| / \ ")
print("| ")
print("Adios my friend , i hope you rest in peace in HELL")
exit()
else:
print(" it seems the noose tightens around your neck and its getting")
print(" hard to talk but i just need 'yes' or 'no' for answer")
repeat()
repeat()
def core_game(self):
while guesses_left > 0 and not dash_guess == word:
guess = input("so tell me that snitch name letter by letter : ")
if guess != 1:
print("NOPE , i need you to spell the name of that rat")
game = Hangman()
game.entrance()
I've also applied some stylistic corrections and corrected the indentation here. You've got a logic bug left, as well, but I'll leave that as an exercise for you to figure out.
I'm making a guessing game or computer science in school where the number to guess is seven. I have tried using while loops and if elif else statements but it doesn't seem to want to make a conditional loop My code is as follows:
guess=int(input("Guess a number!"))
var=1
while var==1:
if guess !=7:
print("Try again")
else:
print("Well done")
Any help would be appreciated thanks. I need it in about a week and a half's time.
If you're trying to allow your player to continuously guess the input needs to be at the top of the while loop, before the conditional-branch
while(True):
guess = input("Make a guess: ")
if(guess == 7):
print(guess,"was correct!")
break
else:
print("Nope. Guess again.")
Of course, you could make it more interesting in a variety of ways.
guess=int(input("Guess a number!"))
var=1
while var==1:
if guess !=7:
print("Try again")
guess=int(input("Guess a number!"))
else:
print("Well done")
var=0 #set var to 0, to exit the loop
Try this. You need to exit the loop, and to do that, var needs to be set to 0.
I am new to python I am trying to code this, I am asking a question, hence the "Are you a mutant" and depending on if the user responds with a yes or no it should come up the respective output but it works only for yes but not for no. how do i make it work for the elif output?
print("Are you a mutant?")
answer = input()
if 'Yes':
print("Your application to Xavier's School for Gifted Youngsters has been accepted")
elif 'No':
print("Your application was not successful on this occassion")
`
You need to compare the variable that stores the users input with the thing you are comparing it to. In this case using the ==. Below is revised code off your example:
print("Are you a mutant?")
answer = input()
if answer == 'Yes':
print("Your application to Xavier's School for Gifted Youngsters has been accepted")
elif answer == 'No':
print("Your application was not successful on this occassion")
You have to write raw_input instead of input. 'Input' just takes the text value but 'raw_input'get the input as a string.
If you are using python2, then follow the code below:
print("Are you a mutant?")
answer = raw_input("Yes/No: ")
if answer == "Yes":
print("Your application to Xavier's School for Gifted Youngsters has been accepted")
elif answer == "No":
print("Your application was not successful on this occasion")
In python3 raw_input() was renamed to input(). Then follow the code below:
print("Are you a mutant?")
answer = input("Yes/No: ")
if answer == "Yes":
print("Your application to Xavier's School for Gifted Youngsters has been accepted")
elif answer == "No":
print("Your application was not successful on this occasion")
This question references info from my previous question:
Text Game - If statement based of input text - Python
So, now I have this:
#Choice Number1
def introchoice():
print()
print("Do you 'Hesitate? or do you 'Walk forward")
print()
def Hesitate():
print()
print("You hesistate, startled by the sudden illumination of the room. Focusing on the old man who has his back turned to you. He gestures for you to come closer. \n ''Come in, Come in, don't be frightened. I'm but a frail old man'' he says.")
print()
#
def Walk():
print()
print("DEFAULT")
print()
#
def pick():
while True:
Input = input("")
if Input == "Hesitate":
Hesitate()
break
if Input == "Walk":
Walk()
break
#
#
pick()
#-#-#-#-#-#-#-#-#-#-#-#-#
#Clean-up
#-#-#-#-#-#-#-#-#-#-#-#-#
Now what I want to do is this;
def pick():
while True:
Input = input("")
if Input == "Hesitate":
Hesitate()
break
if Input == "Walk":
Walk()
break
if Input is not "Walk" or "Hesitate":
print("INVALID")
break
#
#
pick()
#-#-#-#-#-#-#-#-#-#-#-#-#
#Clean-up
#-#-#-#-#-#-#-#-#-#-#-#-#
Now that I have the game determine specific text inputs, I want it to be able to detect if the input was not one of the choices. That way, as typed in the above code, If the input text is not either "Walk" or "hesitate", print Text "INVALID"
How would I do this exactly?
I guess you want to still receiving input if it is "invalid", so the break statements have to be inside the ifs. Otherwise, the loop will only iterate one time.
Also, I would recommend you to use a if-elif-else structure so your code looks more organized.
You can't use is or is not in this case, because these operators are used to check if the objects are the same (same reference). Use the operators == and != to check for equality.
while True:
my_input = input("> ")
if my_input == "Hesitate":
hesitate()
break
elif my_input == "Walk":
walk()
break
else:
print("INVALID")
Notes:
I would recommend you to follow Python naming conventions. Names of variables and methods should start in lowercase.