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
Related
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.
When I run this function it returns None and a reference instead of the intended value.
I already am using "return" which is supposed to make the function return the intended value, but like I said it is still returning a reference.
def querydate():
querydate = int(input("Please enter a year between 2000 and 2017 for
processing injury data "))
numberchoice0 = querydate
while numberchoice0 is querydate:
try:
while int(numberchoice0)<2000:
print("oopsie, that year is before those for which this
query searches.")
quit()
while int(numberchoice0)>2017:
print("oopsie, that year is after those for which this
query searches.")
quit()
except ValueError:
print ('That was not an integer!')
affirmations = ('YES', 'Y')
answer = input("Do you want to continue? (Yes/Y/y):\n")
if answer.strip().upper() in affirmations:
continue
else:
return querydate()
print(querydate())
def verify():
verify = input("please enter 'yes' or 'no' ")
if verify == "no":
print("You should be more careful about inputting data!")
quit()
while verify != "yes":
print(verify, "is not an appropriate input. If you answered 'YES'
or 'Yes' please enter 'yes'")
continue
if verify == "yes":
print("Great! Let us continue")
verify()
I expect the output to be a number between 2000 and 2017, but when I print querydate() it returns "None", and when I reference querydate() with verify() it actually returns <function querydate at 0x000001F1DCFB9A60>
return does not make the function return the intended value, one has to explicitly specify it according to what one wants to return.
You wanted the output from 2000 to 2017 so you need to return the value that returns this.
def querydate():
qDate = int(input("Please enter a year between 2000 and 2017 for
processing injury data "))
numberchoice0 = qDate
while numberchoice0 is qDate:
try:
while int(numberchoice0)<2000:
print("oopsie, that year is before those for which this
query searches.")
quit()
while int(numberchoice0)>2017:
print("oopsie, that year is after those for which this
query searches.")
quit()
except ValueError:
print ('That was not an integer!')
affirmations = ('YES', 'Y')
answer = input("Do you want to continue? (Yes/Y/y):\n")
if answer.strip().upper() in affirmations:
continue
else:
return qDate #returning the integer instead of None
print(querydate())
def verify():
verify = input("please enter 'yes' or 'no' ")
if verify == "no":
print("You should be more careful about inputting data!")
quit()
while verify != "yes":
print(verify, "is not an appropriate input. If you answered 'YES'
or 'Yes' please enter 'yes'")
continue
if verify == "yes":
print("Great! Let us continue")
verify()
Also since you had returned explicitly nothing, referencing querydate() with verify() should return the address reference but if you had returned an integer like querydate or numberchoice0 then it returns a year from range 2000-2017.
Edit:
As far as your TypeError: 'int' object is not callable is concerned, it happens due to the naming of local variable and function name being same. So at first the identifier querydate refers to the function querydate() then it goes inside the function and now it refers to a variable querydate and no longer refers to the function when assigning of var querydate is encountered. So changing the name of one of the identifiers fixes the issue.
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")