How do I have my program actually activate the quiz? - python

What is wrong with my coding. It is a simple conditional statement where you say yes or no to taking the quiz. If you type yes, you begin answering questions and if you type no, it just exits out of the function.
play=input("\v Do you want to take the quiz or not? Yes or No? ").lower
if play == "no":
print("That's too bad")
quit(main())
question_num=0
green_point=0
mean_point=0
if play=="yes":
print("Great! Let us Begin!")
for questions,answers in QUESTIONS:
playeranswer=input("{} " .format(questions))
But the program just ends once you type in an answer for play. I thought it was pretty clear what is supposed to happen. Why is it not doing anything?

1- the indention is wrong with your coding
2- you forgot the () after .lower
your code should be like this :
play = input("\v Do you want to take the quiz or not? Yes or No? ").lower()
if play == "no":
print("That's too bad")
quit(main())
question_num = 0
green_point = 0
mean_point = 0
elif play == "yes":
print("Great! Let us Begin!")
for questions, answers in QUESTIONS:
playeranswer = input("{} " .format(questions))

Actually it's not just the indentation. When you do this:
play = input("\v Do you want to take the quiz or not? Yes or No? ").lower
It actually sets play to be the function lower, not the lowercase string that results from calling lower(). You need to add parentheses:
play = input("\v Do you want to take the quiz or not? Yes or No? ").lower()
if play == "no":
print("That's too bad")
quit(main())
question_num = 0
green_point = 0
mean_point = 0
elif play == "yes":
print("Great! Let us Begin!")
for questions, answers in QUESTIONS:
playeranswer = input("{} " .format(questions))

Related

How can i get my code to work correctly? user input is not working with if, elif, and else

if, elif, and else are not working with my inputs correctly. I am trying to make it so you have to pass all three questions to gain access to my fake club. but the problem is if you answer the first two questions incorrectly and the third one correctly, you still gain access to the club. also, this is an assignment for school, and my teacher said i was missing a heading, and my if statements contained no logical statements to actually weed out or accept potential users. please, help.
def main():
age = input("are you younger than 21?")
game = input("what is the game of the day?")
cool = input("what do you think about school?")
print("Welcome to Kidz Only.")
print("are you cool enough to enter?")
if( age == "yes"):
print("question one is done.")
if ( game == "super mario bros"):
print("question two is through.")
if ( cool == "i have mixed feelings"):
print("question three is complete.")
print("You have passed the quiz. Welcome to Kidz Only!")
else:
print("Sorry, but you may not enter Kidz Only.")
main()
Reason to that would be because you are saying only if ( cool == "i have mixed feelings") then display you are allowed to join the "cool kids".
You can also shorten your code by using and.
def main():
age = input("are you younger than 21?")
game = input("what is the game of the day?")
cool = input("what do you think about school?")
print("Welcome to Kidz Only.")
print("are you cool enough to enter?")
if( age == "yes") and (game == "super mario bros") and ( cool == "i have mixed feelings"):
print("question three is complete.")
print("You have passed the quiz. Welcome to Kidz Only!")
else:
print("Sorry, but you may not enter Kidz Only.")
main()
The last if is tied to the else statement so even if the first two are wrong and the last one is correct, it will allow you to enter. A better logic would be to have conditionals for each of those requirements.
def main():
age = input("are you younger than 21?")
game = input("what is the game of the day?")
cool = input("what do you think about school?")
print("Welcome to Kidz Only.")
print("are you cool enough to enter?")
hasAge = False
hasGame = False
hasCool = False
if( age == "yes"):
hasAge = True
print("question one is done.")
if ( game == "super mario bros"):
hasGame = True
print("question two is through.")
if ( cool == "i have mixed feelings"):
hasCool = True
print("question three is complete.")
if (hasAge and hasGame and hasCool):
print("You have passed the quiz. Welcome to Kidz Only!")
else:
print("Sorry, but you may not enter Kidz Only.")
main()
Although this results in a more lengthy code, it helps you see the logic better

How can I have my input be typed out again via using while loops and if statements?

So i have this simple problem and I am not so sure what to do even after trying it for quite sometime.
combinations = ["rock","papers","scissors"]
ask = str(input("Do you want to play rock papers scissors?"))
while ask:
if ask.lower() == "yes":
print("ok")
break
elif ask.lower() == "no":
print("okay :(")
break
else:
ask = input("So is it a yes or no ?!")
break
my issue is that I am not so sure what to type such that my input user has to type either yes or no instead of typing something else. Additionally, when I tried to run the program, when I typed yes/no it jumped straight to the else statement instead of the if statement.. someone pls help :( (am just a noob programmer)
I would remove the break in the else statement so that your program keeps looping until the user inputs yes or no.
combinations = ["rock","papers","scissors"]
ask = str(input("Do you want to play rock papers scissors?"))
while ask:
if ask.lower() == "yes":
print("ok")
break
elif ask.lower() == "no":
print("okay :(")
break
else:
ask = input("So is it a yes or no ?!")

How to make a python program loop/repeat

I have a python programming spelling game for children, and I need to make it loop/restart if the player clicks yes once they have finished the game, and exit the program if they click no.
This is the top of my programming.
#Declare Constants and Variables
Score = 0
PlayerAnswer = 0
playOn = 0
while playOn != "Yes":
and this is the end, where I want the player to be able to repeat the game if they click yes on the easygui buttonbox.
playOn = easygui.buttonbox ("Do you want to play again?", choices = ["Yes", "No"])
if playOn == "Yes":
Score = 0 #resets score count, if player wants to play again
elif playOn == "No":
easygui.msgbox ("Bye for now. Hope you'll play the game again soon!")
whenever I test it and click yes, the program closes anyway.
while playOn != "Yes":
playOn = easygui.buttonbox ("Do you want to play again?", choices = ["Yes", "No"])
if playOn == "Yes":
Score = 0 #resets score count, if player wants to play again
elif playOn == "No":
easygui.msgbox ("Bye for now. Hope you'll play the game again soon!")
In Python, the code body needs to be indented for it to be interpreted as inside a code block.
In other languages such as C# as long as the codes are inside method{ //code inside here} then the codes will run inside the method.
while (playOnBool):
playOn = easygui.buttonbox ("Do you want to play again?", choices = ["Yes", "No"])
if playOn == "Yes": playOnBool = True
else: playOnBool = False
You need to wrap your code with a while loop.
The code at the end is not in the 'while' loop at the top.
Since Python goes by indentation, the program will end after the playOn variable is set at the end.
I assume there must be code in the middle, at least a 'pass', otherwise Python will give an indented block error.

How to make python accept random input

I'm currently working on a python project and I need it to accept random input from users at some point.
So, if I have, for example:
def question_one () :
answer_one = raw_input ('How many days are there in a week? ').lower()
try:
if answer_one == 'seven' or answer_one == '7' :
question_2()
Everything works wonders. But how can I make python accept random input, as in
def question_two () :
answer_two = raw_input ('What´s your mother´s name? ').lower()
try:
if answer_two == ***I have no idea how to code this part*** :
question_3()
In this case, I would need python to accept any input and still take the user to the next question. How could I do it?
Just remove the if clause then.
If the input doesn't have to be of a specific form, or have some particular property, then there's no need for the if statement, or even the try.
def question_two():
answer_two = raw_input("What's your mother's name?").lower()
question_3()
If you want to be able to re-ask the question, if they don't get it right then you can loop back to it like so. The only acceptable answer for this question is "yes", or "YES", etc..
If they don't answer correctly it will ask them again,till they get it right.
def question1():
answer1 = raw_input("Do you like chickens?")
answer1 = answer1.lower()
if answer1 == 'yes':
print "That is Correct!"
question2()
else:
question1()
If you want them to be able to go on to the next question even if they get it wrong, you can do like so:
def question1():
answer1 = raw_input("Do you like chickens?")
answer1 = answer1.lower()
if answer1 == 'yes':
print "That is Correct!"
else:
print "Next question coming!"
question2()
def question2():
answer2 = raw_input("How many days in a week?")
answer2 = answer2.lower()
if answer2 == '7' or answer2 == "seven":
print "That is Correct!"
else:
print "Sorry,that was wrong"
question3()

How do I use 2 loops, and if it is false, repeat the program indefinetely

Right now I am doing the tutorial "Dragon Realm" from this site http://inventwithpython.com/chapter6.html
I understand what it's all doing slightly but that is not my problem. At the end I'm wanting to add a bit of code that if the player says no, it says, as I currently have it, "too bad *!" and move back up to the beginning of the program. I've gotten it to do that, but once it goes through the second time and i get to the input of whether you want to try again whether or not you type yes or no it just ends the program. I've tried multiple combinations of while, if/else, while True, while False and I am not getting the results I am wanting. I don't understand how you just keep it to keep going? It's probably really simple but I can't figure it out.
this is the code for the program.
import random
import time
def displayIntro():
print('You are in a land full of dragons. In front of you,')
print('you see two caves. In one cave, the dragon is friendly')
print('and will share his treasure with you. The other dragon')
print('is greedy and hungry, and will eat you on sight.')
print()
def chooseCave():
cave = ''
while cave != '1' and cave != '2':
print('Which cave will you go into? (1 or 2)')
cave = input()
return cave
def checkCave(chosenCave):
print('You approach the cave...')
time.sleep(2)
print('It is dark and spooky...')
time.sleep(2)
print('A large dragon jumps out in front of you! He opens his jaws and...')
print()
time.sleep(2)
friendlyCave = random.randint(1, 2)
if chosenCave == str(friendlyCave):
print('Gives you his treasure!')
else:
print('Gobbles you down in one bite!')
playAgain = 'yes'
while playAgain == 'yes' or playAgain == 'y':
displayIntro()
caveNumber = chooseCave()
checkCave(caveNumber)
print('Do you want to play again? (yes or no)')
playAgain = input()
You could add simply,
if 'n' in playAgain:
print "too bad"
playAgain = 'yes'
At the end (inside your while loop)
By the way, these two lines can be combined:
print('Do you want to play again? (yes or no)')
playAgain = input()
as simply:
playAgain = input('Do you want to play again? (yes or no)')
Because input will display the string argument when asking for input.

Categories

Resources