yes or no output in python - python-3.x

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")

Related

If statement issue with basic python program [python]

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.

Strings not selected properly in if comparison

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.

Weird problem with calling functions in Python

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.

how to insert a score counter in python

def quiz(demand,correct):
print(" ")
Score=0
Answer=input(demand)
Answer=Answer.lower()
if Answer!="y" and Answer!="n":
print("I did not understand the answer")
quiz(demand,correct)
elif Answer==correct:
print("correct answer")
Score=Score+1
return Score
else:
print("wrong answer")
demand1="the Napoleon's horse is white? y/n: "
correct1="y"
quiz(demand1,correct1)
demand2="berlusconi is president of italy? y/n: "
correct2="n"
quiz(demand2,correct2)
print("score:",Score)
I'm trying to insert a score counter,
why does not it work?
can someone give me the solution?
I'm sorry for my bad english.
The issue is scope, score gets set to zero every time you call quiz
The quickest solution is as follows
Score=0
def quiz(demand,correct):
print(" ")
<everything else is the same>
Call the function and assign the value to a variable and print. Note that the variable scope is local to a function and calling it from outside requires some special declaration global.
Score = 0
def quiz(demand,correct):
global Score

text-based adventure help in python 3.x

I am a beginner programmer. I want to create a game where user input affects the course of the game. I am kind of stuck on the very beginning.
def displayIntro():
print("You wake up in your bed and realize today is the day your are going to your friends house.")
print("You realize you can go back to sleep still and make it ontime.")
def wakeUp():
sleepLimit = 0
choice = input("Do you 1: Go back to sleep or 2: Get up ")
for i in range(3):
if choice == '1':
sleepLimit += 1
print("sleep")
print(sleepLimit)
if sleepLimit == 3:
print("Now you are gonna be late, get up!")
print("After your shower you take the direct route to your friends house.")
elif choice == '2':
print("Woke")
whichWay()
else:
print("Invalid")
def whichWay():
print("After your shower, you decide to plan your route.")
print("Do you take 1: The scenic route or 2: The quick route")
choice = input()
if choice == 1:
print("scenic route")
if choice == 2:
print("quick route")
displayIntro()
wakeUp()
i have a few bugs and I've tried to work them out on my own but I'm struggling.
1) I only want the player to be able to go back to sleep 3 times and on the third i want a message to appear and another function to run (havent made yet).
2) if the player decides to wake up i want whichWay() to run and it does but instead of exiting that for loop it goes right back to that loop and asks if the player wants to wake up again i have no idea how to fix this.
3) is there a better way i can go about making a game like this?
Thank you for your time and hopefully your answers.
The code below should work.
1. I moved the line 'choice = input("Do you 1: Go back to sleep or 2: Get up ")' into the for loop.
2. I added a break statement at the end of the elif block.
def wakeUp():
sleepLimit = 0
for i in range(3):
choice = input("Do you 1: Go back to sleep or 2: Get up ")
if choice == '1':
sleepLimit += 1
print("sleep")
print(sleepLimit)
if sleepLimit == 3:
print("Now you are gonna be late, get up!")
print("After your shower you take the direct route to your friends house.")
elif choice == '2':
print("Woke")
whichWay()
break
else:
print("Invalid")

Resources