Timing in python 3 - python-3.x

Hello So I am making a simple timed addition program for the little kids in my school. How do I make it so that after 5 seconds it will do:
print ("Try again")
here is my code
running = True
import time
from random import randint
while running:
print ("Hello world!")
time.sleep(1.5)
print ("Thank you for running this program, if not I would have been trapped here for like EVER!")
time.sleep(2.5)
print ("My name is ιllιlııllııllııllıιllιlı.")
time.sleep(2.5)
print ("Crap! It seems like my system is still bugged I cant diplay my name")
time.sleep(2.5)
print ("You are going to have to help me")
time.sleep(2.5)
print (5000 * '-')
time.sleep(2.5)
print ("Here is how you are going to have to help me, I need you to solve math problems in order to get me out of here, I am going to give you 5 seconds to solve them!")
time.sleep(10)
print ("Ok this is going to start in 3")
time.sleep(1)
print ("2")
time.sleep(1)
print ("1")
time.sleep(0.5)
print ("GO")
add1 = (randint(0,99))
print ("What's", add1, "+", add2, "?")
addansm = int(input("Write your answer here:"))
addans = add1 + add2
addans = int()
if addans and addansm == addans :
print ("Correct!")
time.sleep(2.5)
running2 = True
running = False
else :
print ("Try again!")
running = False
Thank you for your help, Some extra information is that as soon as
print ("What's", add1, "+", add2, "?")
Happens I want the 5 second timer to start
Thanks you very much!

Related

print outside while prints twice python3

I have this assignment I have been working on
temperatures = []
def decision():
answer = input("Do you want to enter a temperature?" +
"\"y\" for yes. \"n\" for no: ")
getTemp(answer)
def getTemp(answer):
while answer == "y" or answer == "Y":
temp = int(input("Enter a temperature: "))
temperatures.append(temp)
print("\nTemperature Entered!\n")
answer = " "
decision()
print("Temperatures entered: ", temperatures)
def main():
decision()
main()
The problem is when I enter a temperature then press n to exit the while loop, the final output is more than one print statement. For example if I input:(y's == yes)
y
3
y
5
n
the output is
Temperatures entered: [3,5]
Temperatures entered: [3,5]
Temperatures entered: [3,5]
Any help would be great...Thanks
The issue is that getTemp is being called multiple times, due to it calling decision, which in turn calls getTemp. Instead, you should only print the temperatures after you exit the above chain, so you should move the print to after you call decision in main, so main should be:
def main():
decision()
print("Temperatures entered: ", temperatures)
and getTemp should be
def getTemp(answer):
while answer == "y" or answer == "Y":
temp = int(input("Enter a temperature: "))
temperatures.append(temp)
print("\nTemperature Entered!\n")
answer = " "
decision()
You are recursing. If you ended up wanting to enter, say, 500000000 temperatures, you would definitely cause a stack overflow.
Your print executes at the end of each decision() execution. I suggest restructuring your code to not recurse (to save yourself from allocating forever) or at the very least put your print statement in your main.
For example, you could do this
temperatures = []
def decision():
while input("Do you want to enter a temperature?\n" +
"\"y\" for yes. \"n\" for no: ") in "yY":
getTemp()
def getTemp():
temp = int(input("Enter a temperature: "))
temperatures.append(temp)
print("\nTemperature Entered!\n")
def main():
decision()
print("Temperatures entered: ", temperatures)
main()

Python 3.6 elif syntax error

using a nested if statement and my indentation seems correct thru out yet still reviving a syntax error.thank you
# FIGHT Dragons
if ch3 in ['y', 'Y', 'Yes', 'YES', 'yes']:
# WITH SWORD
if sword == 1:
print ("You only have a sword to fight with!")
print ("You quickly jab the Dragon in it's chest and gain an advantage")
time.sleep(2)
print ("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
print (" Fighting... ")
print (" YOU MUST HIT ABOVE A 5 TO KILL THE DRAGON ")
print ("IF THE DRAGON HITS HIGHER THAN YOU, YOU WILL DIE")
print ("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
time.sleep(2)
fdmg1 = int(random.randint(3, 10))
edmg1 = int(random.randint(1, 5))
print ("you hit a", fdmg1)
print ("the dragon hits a", edmg1)
time.sleep(2)
if edmg1 > fdmg1:
print ("The drgon has dealt more damage than you!")
complete = 0
return complete
this is where i run into a syntax error
elif fdmg1 < 5:
print ("You didn't do enough damage to kill the drgon, but you manage to escape")
complete = 1
return complete
else:
print ("You killed the drgon!")
complete = 1
return complete
Your return must be at the end of the if...elif...else statement. This works :
if edmg1 > fdmg1:
print ("The drgon has dealt more damage than you!")
complete = 0
elif fdmg1 < 5:
print ("You didn't do enough damage to kill the drgon, but you manage to escape")
complete = 1
else:
print ("You killed the drgon!")
complete = 1
return complete
Note that if the first if-condition is True, Python won't check for the subsequent ones.

Random word game python 3.5

Hi i'm completely new to programming and have been trying to teach myself python, I have been trying to create a program that selects a word then shuffles the letters and prompts the user to input their guess within 3 tries. The problem I'm having is when a wrong answer is input it reshuffles the letters of the chosen word or returns a completely different word, here is my code:
import random
import sys
##Welcome message
print ("""\tWelcome to the scrambler,
select [E]asy, [M]edium or [H]ard
and you have to guess the word""")
##Select difficulty
difficulty = input("> ")
difficulty = difficulty.upper()
##For counting number of guesses it takes
tries = 0
while tries < 3:
tries += 1
##Starting the game on easy
if difficulty == 'E':
words = ['teeth', 'heart', 'police', 'select', 'monkey']
chosen = random.choice(words)
letters = list(chosen)
random.shuffle(letters)
scrambled = ''.join(letters)
print (scrambled)
guess = input("> ")
if guess == chosen:
print ("Congratulations!")
break
else:
print ("you suck")
else:
print("no good")
sys.exit(0)
As you can see I've only gotten as far as easy, I was trying to do it piece by piece and managed to overcome other problems but I can't seem to fix the one I'm having. Any help would be appreciated with the problem I'm having or any other issues you may spot in my code.
A few improvements and fixes for your game.
import random
import sys
# Game configuration
max_tries = 3
# Global vars
tries_left = max_tries
# Welcome message
print("""\tWelcome to the scrambler,
select [E]asy, [M]edium or [H]ard
and you have to guess the word""")
# Select difficulty
difficulty = input("> ")
difficulty = difficulty.upper()
if difficulty == 'E':
words = ['teeth', 'heart', 'police', 'select', 'monkey']
chosen = random.choice(words)
letters = list(chosen)
random.shuffle(letters)
scrambled = ''.join(letters)
else:
print("no good")
sys.exit(0)
# Now the scrambled word fixed until the end of the game
# Game loop
print("Try to guess the word: ", scrambled, " (", tries_left, " tries left)")
while tries_left > 0:
print(scrambled)
guess = input("> ")
if guess == chosen:
print("Congratulations!")
break
else:
print("You suck, try again?")
tries_left -= 1
Tell me if you don't understand something, I would be a pleasure to help you.

Variable not Defined in Python 3.5.1

I am a new coder with Python and I was wondering how I can fix this bug. Every time i put in the correct input in the code that I have, it spits out an error message, like so.
The code
total = 12
print ("I will play a game, you will choose 1, 2, or 3, and I will do the same, and I should always win, do you want to play?")
yesnoA = input("Yes or No?")
if yesnoA == yes:
print ("Yay, your turn!")
turnAA = input('Your First Move')
if turnAA == 1:
print ("I choose 3")
total = total - 4
print ("Total = ",total)
else:
if turnAA == 2:
print ("I choose 2")
total = total - 4
print ("Total = ",total)
else:
if turnAA == 3:
print ("I choose 1")
total = total - 4
print ("Total = ",total)
else:
print ("Cheater, try again")
else:
yesnoB = input("Ok, you sure?")
if yesnoB == yes:
print ("Yay, your turn")
turnAA = input('Your First Move')
if turnAA == 1:
print ("I choose 3")
total = total - 4
print ("Total = ",total)
else:
if turnAA == 2:
print ("I choose 2")
total = total - 4
print ("Total = ",total)
else:
if turnAA == 3:
print ("I choose 1")
total = total - 4
print ("Total = ",total)
else:
print ("Cheater, try again")
else:
print ("Well, goodbye")
The Output
Yes or No?yes
Traceback (most recent call last):
File "C:/Users/*user*/Desktop/Code/Python/Nim Game.py", line 5, in <module>
if yesnoA == yes:
NameError: name 'yes' is not defined
This is in version 3.5.1
You need to either declare a variable yes with a value 'yes', or compare your variable yesnoA with a string 'yes'. Maybe something like this:
if yesnoA.lower() == 'yes': # using lower(), so that user's input is case insensitive
# do the rest of your work
Your code afterwards has some more issues. I will give you a clue. input always returns the user's input as string. So if you need integer from user, you will have to convert the user input into integer using int(your_int_as_string) like so:
turnAA = int(input('Your First Move'))
# turnAA is now an integer, provided the user entered valid integer value
Your take from this question on SO:
Look at the Traceback. It says clearly which line the error is in, and also what the error is. In your case it is NameError
Take a look at docs for NameError
Study this tutorial. It will help you get used to with some basic errors commonly encountered.
You have not defined the variable yes. You should do something like:
yes = "Yes"
At the start of the code
You're attempting to compare yesnoA to a variable named yes, which, indeed was not defined, instead of the string literal 'yes' (note the quotes!). Add the quotes and you should be fine:
if yesnoA == 'yes':
# Here --^---^

Python variable checking

How would I get Python to check a variable to see if it is an integer and below a specific number then carry on depending on the result perhaps breaking from a loop.
so something roughly like this:
x = input
if x is integer AND below 10:
print ("X is a number below 10")
break from loop
elif x is NOT an integer:
print ("X is not an integer try again")
ask again
Else:
print ("Error")
ask again
pretty sure this works :D thanks
while True:
x = input("Enter Something: ") #user input
if x.isdigit() == True:
if (int(x)>=1) and (int(x)<=2): #if it is 1 or 2
print (x)
break
elif (int(x)>2): #or if its above 2
print ("To Large")
else:
print ("Else")
elif x.isdigit() == False: #data non integer
print ("please try again")
else:
print ("ERROR") #error with input
print ("Hello") #show the loop broke correctly

Resources