import time
import random
info = """welcome to contest!..
pls answer question as quick as possible you can
pls enter small letter...\n"""
print(info)
questions = {"2 * 2 ?":"4",
"what is the capital city of turkey?":"ankara",
"what is the king of jungle?":"lion",
"what is the meaning of book in turkish language?":"kitap",
"who is the foundation of turkish government":"atatürk",
"what is the most popular drink in turkey?":"raki",
"pls tell us a hero as comic":"temel"}
correct = 0
wrong = 0
blank = 0
current_time = time.time() #system time
allowed_time = 25 #total time to reply the question
for i in random.sample(list(questions), 5):
question = questions[i]
if time.time() < current_time+allowed_time:
answer = input("1. soru --> {} : ".format(i))
if answer == question:
correct += 1
elif answer == "":
blank += 1
else:
wrong += 1
print()
print("right answer :", correct)
print("wrong answer :", wrong)
print("blank answer :", blank)
Please see my survey code above. It's selecting random 5 question in total time 25 seconds. But, I'd like to make it time option for every single question.
For example, questions must be replied with in ten seconds otherwise change question automatically.
Could you help on how to do that?
here's the steps to proceed(algorithm):
Add a timer to each question.
Run a counter or a timer inside the for loop, then, for every iteration passed, store the time in a variable x, check if x is less than 10.
Check whether it is answered within 10 seconds.
Iterate to the Next question, this i+1.
If NOT, jump to the next question.
The input() function - by its definition - waits for the input infinitely. Probably there is no cross-platform solution for it.
For *nix operating systems you may do this:
Create another thread with the timer which will interrupt the main thread after some time limit.
You may define a function for your input which will do it:
import thread # _thread for Python 3.x
import threading
def timed_input(prompt, timeout=25.):
timer = threading.Timer(timeout, thread.interrupt_main)
answer = ""
try:
timer.start()
answer = input(prompt)
except KeyboardInterrupt:
pass
timer.cancel()
return answer
and then use it instead of the standard input() function in your code (and of course without your original attempts with the time module).
Probably the most simple and the most elegant (from the programmer's point of view) is simply to keep infinitely time for an answer but then reject it if it was answered after the given time limit:
start_time = time.time()
answer = input("1. soru --> {} : ". format(i))
if time.time() - start_time > allowed_time:
# code for rejecting the answer
So instead of your for loop use this (a tested one, and with an improvement):
for j, i in enumerate( random.sample(list(questions), 5) ): # Slight change
question = questions[i]
start_time = time.time()
answer = input("{}. soru --> {} : ". format(j + 1, i)) # Sligth change
if time.time() - start_time > allowed_time:
print(" Time-out: You didn't answer quickly enough.")
answer = ""
if answer == question:
correct += 1
elif answer == "":
blank += 1
else:
wrong += 1
Not that I slightly changed your for statement and the answer = ... for the sake of printing correct ordinal numbers of questions (yours were always 1.)
Related
Hi this is my first time using this website so if I mess up the formatting please bear with me. I'm currently stuck at one of the practice projects in Chapter 8 and I can't seem to find any decent answers for this project.
This is the question:
**To see how much PyInputPlus is doing for you, try re-creating the multiplication quiz project on your own without importing it. This program will prompt the user with 10 multiplication questions, ranging from 0 × 0 to 9 × 9. You’ll need to implement the following features:
-If the user enters the correct answer, the program displays “Correct!” for 1 second and moves on to the next question.
-The user gets three tries to enter the correct answer before the program moves on to the next question.
-Eight seconds after first displaying the question, the question is marked as incorrect even if the user enters the correct answer after the 8-second limit.**
import random, time
num_of_ques = 10
tries = 3
correct_ans = 0
start_time = 0
elapsed = 0
for ques_num in range(num_of_ques):
num1 = random.randint(0, 9)
num2 = random.randint(0, 9)
result = num1 * num2
while tries > 0:
if start_time == 0:
start_time = time.time()
while True:
ans = input(f'{ques_num+1}: {num1} * {num2} = ? ')
try:
ans = int(ans)
break
except ValueError:
print('Not a number, please try again.')
elapsed = time.time() - start_time
if ans == result and elapsed<=8:
print('Correct!')
correct_ans += 1
time.sleep(1)
break
elif ans == result and elapsed>8:
print("Time's up!")
break
else:
print('Incorrect, try again!')
tries -= 1
ques_num += 1
time.sleep(1)
print('Score: %s / %s' % (correct_ans, num_of_ques))
I can't really seem to get the 8 second timer part correct. I'm also a complete beginner at python so I try to use the modules that are taught in the book. Are there any other places that I can put the (elapsed = time.time() - start_time) so the code works? Any help would be appreciated.
(using Python 3.9 btw)
Hello fellow programmers! I am a beginner to python and a couple months ago, I decided to start my own little project to help my understanding of the whole development process in Python. I briefly know all the basic syntax but I was wondering how I could make something inside a function call the end of the while loop.
I am creating a simple terminal number guessing game, and it works by the player having several tries of guessing a number between 1 and 10 (I currently made it to be just 1 to test some things in the code).
If a player gets the number correct, the level should end and the player will then progress to the next level of the game. I tried to make a variable and make a true false statement but I can't manipulate variables in function inside of a while loop.
I am wondering how I can make it so that the game just ends when the player gets the correct number, I will include my code down here so you guys will have more context:
import random
import numpy
import time
def get_name(time):
name = input("Before we start, what is your name? ")
time.sleep(2)
print("You said your name was: " + name)
# The Variable 'tries' is the indication of how many tries you have left
tries = 1
while tries < 6:
def try_again(get_number, random, time):
# This is to ask the player to try again
answer = (input(" Do you want to try again?"))
time.sleep(2)
if answer == "yes":
print("Alright!, well I am going to guess that you want to play again")
time.sleep(1)
print("You have used up: " + str(tries) + " Of your tries. Remember, when you use 5 tries without getting the correct number, the game ends")
else:
print("Thank you for playing the game, I hope you have better luck next time")
def find_rand_num(get_number, random, time):
num_list = [1,1]
number = random.choice(num_list)
# Asks the player for the number
ques = (input("guess your number, since this is the first level you need to choose a number between 1 and 10 "))
print(ques)
if ques == str(number):
time.sleep(2)
print("Congratulations! You got the number correct!")
try_again(get_number, random, time)
elif input != number:
time.sleep(2)
print("Oops, you got the number wrong")
try_again(get_number, random, time)
def get_number(random, try_again, find_rand_num, time):
# This chooses the number that the player will have to guess
time.sleep(3)
print("The computer is choosing a random number between 1 and 10... beep beep boop")
time.sleep(2)
find_rand_num(get_number, random, time)
if tries < 2:
get_name(time)
tries += 1
get_number(random, try_again, find_rand_num, time)
else:
tries += 1
get_number(random, try_again, find_rand_num, time)
if tries > 5:
break
I apologize for some of the formatting in the code, I tried my best to look as accurate as it is in my IDE. My dad would usually help me with those types of questions but it appears I know more python than my dad at this point since he works with front end web development. So, back to my original question, how do I make so that if this statement:
if ques == str(number):
time.sleep(2)
print("Congratulations! You got the number correct!")
try_again(get_number, random, time)
is true, the while loop ends? Also, how does my code look? I put some time into making it look neat and I am interested from an expert's point of view. I once read that in programming, less is more, so I am trying to accomplish more with less with my code.
Thank you for taking the time to read this, and I would be very grateful if some of you have any solutions to my problem. Have a great day!
There were too many bugs in your code. First of all, you never used the parameters you passed in your functions, so I don't see a reason for them to stay there. Then you need to return something out of your functions to use them for breaking conditions (for example True/False). Lastly, I guess calling functions separately is much more convenient in your case since you need to do some checking before proceeding (Not inside each other). So, this is the code I ended up with:
import random
import time
def get_name():
name = input("Before we start, what is your name? ")
time.sleep(2)
print("You said your name was: " + name)
def try_again():
answer = (input("Do you want to try again? "))
time.sleep(2)
# Added return True/False to check whether user wants to play again or not
if answer == "yes":
print("Alright!, well I am going to guess that you want to play again")
time.sleep(1)
print("You have used up: " + str(tries) + " Of your tries. Remember, when you use 5 tries without getting the correct number, the game ends")
return True
else:
print("Thank you for playing the game, I hope you have better luck next time")
return False
# Joined get_number and find_random_number since get_number was doing nothing than calling find_rand_num
def find_rand_num():
time.sleep(3)
print("The computer is choosing a random number between 1 and 10... beep beep boop")
time.sleep(2)
num_list = [1,1]
number = random.choice(num_list)
ques = (input("guess your number, since this is the first level you need to choose a number between 1 and 10 "))
print(ques)
if ques == str(number):
time.sleep(2)
print("Congratulations! You got the number correct!")
# Added return to check if correct answer is found or not
return "Found"
elif input != number:
time.sleep(2)
print("Oops, you got the number wrong")
tries = 1
while tries < 6:
if tries < 2:
get_name()
res = find_rand_num()
if res == "Found":
break
checker = try_again()
if checker is False:
break
# Removed redundant if/break since while will do it itself
tries += 1
I am creating a python program to deal with Clock and Time. The question arises where user puts input to set a timer for 90 seconds, or any 'n' seconds. Here is the code-
inp=input("How can I help you ? : ")
if ("timer" in inp):
print("setting up...")
a=int(input("Enter no. of seconds : ")
import time
while (a != 0):
print(a)
a-=1
time.sleep(1)
Now I want such an interface to directly catch the specific number of seconds directly from inp variable.
Is it possible?
If you know for sure that the user will input timer x (x is the number of seconds), you can just do the following:
import time
inp=input("How can I help you ? : ")
if ("timer" in inp):
time.sleep(int(inp))
But most of the time you will probably want to do some user input validation.
A quick and dirty solution would be:
inp = input("How can I help you ? : ")
# assuming input is "timer 90"
inp = inp.split(" ")
if inp[0] == "timer":
a = int(inp[1])
import time
while (a != 0):
print(a)
a-=1
time.sleep(1)
Yes it is possible. But you need to specify the protocol first. Like you can input timer and the value in same user input using some delimiter or anything, but that needs to be decided first.
Here is an example where I input timer and value both using , as delimiter.
import time
inp=input("How can I help you ? : ")
# I will input --> timer,10
if ("timer" in inp):
secs = int(inp.split(",")[1].strip())
print("Got timer for sleep is : {0}".format(secs))
while (secs != 0):
print(secs)
secs-=1
time.sleep(1)
So i'm doing a project and have wanted to add some extra features to make it unique.
How do I make it output a timer sort of thing where it would say "It took you (minutes:seconds) to answer this question" after each question and then a overall timer at the end of the exam (Script). Also, how would I add a date on when the exam was taken. What else could i add to make this more unique and users actually interested in doing this. I have simple input features where it asks for their name, class name and then adds up the total score and outputs into a file, named after the class name using 'with' and 'open' commands.
OPERATIONS = [ # this is stating what the operations are.
(operator.add, "+"),
(operator.mul, "*"),
(operator.sub, "-")
]
for _ in range(10):
num1 = random.randint(1,10)#This will randomly select num1 & num2 to be from 1-10
num2 = random.randint(1,10)
op, symbol = random.choice(OPERATIONS) #this will make the symbol equal to the operations and randomly select it by using .choice
print("What is", num1, symbol, num2,"?")
if get_int_input() == op(num1, num2):#this will check if the input is true or false by using ValueError if its false.
print("Well done",name,"you got it correct!")
score += 1
else:
print("Incorrect, better luck next time!")
You can keep state of when you asked the question by using datetime.now() and checking how long it took with datetime.now() - prior time like below. Getting the date is pretty easy as well, datetime.strftime("DD-MM-YYYY") but what I had wrote below has the added benefit of keeping the day as well.
OPERATIONS = [ # this is stating what the operations are.
(operator.add, "+"),
(operator.mul, "*"),
(operator.sub, "-")
]
test_begin = datetime.now()
for _ in range(10):
num1 = random.randint(1,10)#This will randomly select num1 & num2 to be from 1-10
num2 = random.randint(1,10)
op, symbol = random.choice(OPERATIONS) #this will make the symbol equal to the operations and randomly select it by using .choice
start = datetime.now()
print("What is", num1, symbol, num2,"?")
if get_int_input() == op(num1, num2):#this will check if the input is true or false by using ValueError if its false.
print("Well done",name,"you got it correct!")
score += 1
else:
print("Incorrect, better luck next time!")
time_taken = datetime.now() - start
test_length = datetime.now() - test_begin
print(test_length.strftime("DD-MM-YYYY"))
What you want to do is make use of time from the time module.
You want one timer that starts when your entire application runs, and then you want a question timer that you reset after every question.
Here is an example. I leave the refactoring of injecting in to your code up to you.
Make use of a dictionary to store your your results so you can easily look them up later. And then you can just output your dictionary for your results.
from time import time
exam_time = {}
exam_timer_start = time()
for i in range(1, 10):
question_timer_start = time()
answer = input('question')
exam_time["question_{}".format(i)] = time() - question_timer_start
question_timer_end = time() - question_timer_start
print("That question took you {}s to complete".format(question_timer_end))
exam_time["total_exam"] = question_timer_end
print(exam_time)
The validation doesnt work. im not sure why, is there a way to validate a string. The questions asked are endless i need 10 questions to be asked
import random
name=(input("Please enter your name"))
print("welcome",name,"the arithmetic is about to start")
question=0
while question<10:
number=random.randint(1,10)
numbers=random.randint(1,10)
arith=random.choice("+" "-" "/")
if arith=="+":
print(number,arith,numbers)
answer=number+numbers
if arith=="-":
print(number,arith,numbers)
answer=number-numbers
if arith=="/":
print(number,arith,numbers)
answer=number/numbers
while True:
try:
usersanswer= int(input())
except ValueError:
print ("That is not a valid answer")
continue
if usersanswer==answer:
print("correct")
break
else:
print("incorrct")
The validation doesnt work. im not sure why, is there a way to validate a string
I've taking silentphoenix's answer and made it somewhat more pythonic and six'ed.
You should almost never use python2's input, because on top of being massive security hole, it sometimes does things that can be...rather unexpected.
import random
import operator # contains the python operators as functions
try:
input = raw_input # rebind raw_input to input, if it exists
# so I can just use input :P
except NameError:
pass
name = input("Hi, what is your name?\n")
print("Hi {} let's get started! Question 1".format(name))
#Get out of the habit of using string concatenation and use string
#formatting whenever possible. Strings are *immutable*;
#concatenation has to produce a lot temporary strings and is *slow*
#str.join and str.format are almost always better ideas.
#Python does not have a switch-case, so emulating one with a dictionary
operator_mapping = {'+': operator.add,
'-': operator.sub,
'*': operator.mul,
#'/': operator.truediv, #hey, division exists.
#But if you want division to actually work, you'll
#have to introduce a fudge factor :P
}
for i in range(10): # If you're just going for 10 iterations, it should be a for loop
# Brevity :P This is a list comprehension
first_number, second_number = [random.randint(1,10) for _ in range(2)]
oper = random.choice(list(operator_mapping))
answer = operator_mapping[oper](first_number, second_number)
while int(input("{} {} {} = ".format(first_number, oper, second_number))) != answer:
#while abs(float(input("{} {} {} = ".format(first_number, oper, second_number)))-answer) < 0.001: if you want truediv.
print('Wrong answer! try again!')
#If I've left the loop, user has given correct (enough) answer
if i <9: # all but last
print('Well done! Now onto question number {0}'.format(i+2))
print('Well done! You are done!')
In the third line, you ask for input. But a name is a string, so you need raw_input. raw_input takes strings, input only takes numerical values.
Python 2.7 getting user input and manipulating as string without quotations
Nowhere in your code do you update the variable questions, which I am guessing is a counter. You have to update that whenever a question is asked, using question += 1.
Finally, your code at the end does not really make sense. Based off the code, it checks for whether or not it is a string, but then compares it to the answer regardless. The if statement needs to be within the try.
The else statement does not match any outer indentation.
Finally, because of the while True: your code will never exit the loop unless the answer is wrong. At the point the entire program terminates. I see what kind of program you are trying to write, but the parameters for random number generation have to be within some kind of a while question <= 10 loop. As of now, only two lines in the program are being affected by that first while loop.
EDIT: I am working on a good example code. Hopefully this answer will help until I can finish it.
EDIT: Here is code that shows how it works within a while loop.
import random
from random import randint
name = raw_input("Hi, what is your name?\n") # Asks for name
print "Hi " +name+ " let's get started!"
score_count = 0
question_count = 0 # creates counter
while question_count <= 10: # Everything MUST BE WITHIN THIS LOOP
# makes numbers and operator
first_number = randint(1,10)
second_number = randint(1,10)
oper = random.choice("+""-""*")
# determines the problem
if oper == "+":
answer = first_number + second_number
print first_number,second_number,oper
elif oper == "-":
answer = first_number - second_number
print first_number,second_number,oper
elif oper == "*":
answer = first_number*second_number
print first_number, second_number, oper
user_answer = int(raw_input("Your answer: "))
if user_answer != answer:
print 'Wrong answer! try again!'
user_answer = int(raw_input('Your answer: '))
if user_answer == answer: # exits the while loop when the correct answer is given
if question_count < 10:
print 'Well done! Now onto question number {0}'.format(question_count+1)
score_count += 1
elif question_count == 10:
print 'Well done! You are done!'
score_count += 1
else:
print 'Something is wrong.'
question_count += 1 # updates the variable
# GOES BACK TO THE BEGINNING UNTIL question_count IS GREATER THAN OR EQUAL TO 10
print "Your score was: {}".format(score_count)
Happy coding! and best of luck!
hi im Nathan and I saw this post I am 5 years to late but I figured if someone on here is knew to python I have a much easier (in my opinion) way to do this in python 3, the code is below:
import random #random module automatically downloaded when you install python
name = input("Please enter your name ")
print("welcome",name,"the arithmetic is about to start")
question=0
while question<10:
number=random.randint(1,10) #creating a random number
numbers=random.randint(1,10) #creating a random number
list = ["+","-","/"] #creating a list (or sometimes called array)
arith=random.choice(list) #getting random operators from list (+,-,/)
question += 1 #basically means add one to question variable each time in loop
if arith=="+":
print(number,arith,numbers)
answer=number+numbers
elif arith=="-":
print(number,arith,numbers)
answer=number-numbers
elif arith=="/":
print(number,arith,numbers)
answer=number/numbers
answer = int(answer)
#from HERE
useranswer = "initialising this variable"
while useranswer == "initialising this variable":
try:
usersanswer= int(input())
if usersanswer==answer:
print("correct")
break
else:
print("incorrect")
except ValueError:
print ("That is not a valid answer")
#to HERE it is input validation this takes a while to explain in just commenting
#but if you dont know what this is then copy this link https://youtu.be/EG69-5U2AfU
#and paste into google for a detailed video !!!!!!
I hope this helps and is a more simplified commented bit of code to help you on your journey to code in python