I am making a mathematics game which asks a random question from a set list. I want to be able to save user inputs in a new list so i can display their answers at the end of the game.
Can anybody help me figure out how to do this?
import random
counter = 0
score = 0
incorrect = 0
name=input("What is your name?")
print("Hi",name,",welcome to your math quiz!")
questions = ["10*2","4-2","6+12","6*4","12-5","6+54","1*0","3-6","4+0","65-9"]
answers=["20","2","18","24",'7','60','0','-3','4','56']
idx_questions = list(enumerate(questions))
idx_answers = list(enumerate(answers))
random.shuffle(idx_questions)
for idxq, question in idx_questions:
print(question)
ans = input("What is the answer? ")
for idxa, answer in idx_answers:
if idxq == idxa and ans == answer:
print("Correct")
score=score+1
print("Correct Answers=",score)
print("Incorrect Answers=",incorrect)
elif idxq == idxa and ans != answer:
print("Incorrect", answers)
incorrect=incorrect+1
print("Correct Answers=",score)
print("Incorrect Answers=",incorrect)
print("End of quiz")
print(name,"your score is",score,"out of 10")
print(score*10,"%")
This should work:
inputs = []
for idxq, question in idx_questions:
print(question)
ans = input("What is the answer? ")
inputs.append(ans)
for idxa, answer in idx_answers:
# ...
Related
Basically i need to make a game im python that can have a arbitrarily number of people that alternatly answers a multiplication question. But my issue is that i dont know how to keep it running until i hit 30 points. Ive tried to use dict's where the key is the player name and the value is the score. But it stops after the script only has ran once. I tried with a while loop but it went on forever. please help!
import random as r
n = int(input("Insert number of players: "))
d = {}
for i in range(n):
keys = input("Insert player name: ")
#to set the score to 0
values = 0
d[keys] = values
#to display player names
print("Player names are:")
for key in d:
print(key)
for value in d.values():
if value < 30:
random1 = r.randint(0,9)
random2 = r.randint(0,9)
print(f"The two numbers you should multiplie is {random1} and {random2}")
correct = random1*random2
user_inp = input("Insert answer: ")
user_inp = int(user_inp)
if user_inp == correct:
print("Correct!")
d[keys] += 1
else:
print("Wrong!")
d[keys] -= 2
else:
break
I think this will work
winner = False
while not winner :
for value in d.values():
if value < 30:
random1 = r.randint(0,9)
random2 = r.randint(0,9)
print(f"The two numbers you should multiplie is {random1} and {random2}")
correct = random1*random2
user_inp = input("Insert answer: ")
user_inp = int(user_inp)
if user_inp == correct:
print("Correct!")
d[keys] += 1
else:
print("Wrong!")
d[keys] -= 2
else:
winner = True
break
So I tried to make a game where the computer chooses a random 4 digit number out of 10 given numbers. The computer then compares the guess of the user with the random chosen code, and will give feedback accordingly:
G = correct digit that is correctly placed
C = correct digit, but incorrectly placed
F = the digit isn't in the code chosen by the computer
However, the feedback doesn't always output correctly.
Fox example, when I guess 9090, the feedback I get is F C F, while the feedback should consist of 4 letters.... How can I fix this?
#chooses the random pincode that needs to be hacked
import random
pincode = [
'1231', '9997', '8829', '6765', '9114', '5673', '0103', '4370', '8301', '1022'
]
name = None
#Main code for the game
def main():
global code
global guess
#Chooses random pincode
code = random.choice(pincode)
#Sets guessestaken to 0
guessesTaken = 0
while guessesTaken < 10:
#Makes sure every turn, an extra guess is added
guessesTaken = guessesTaken + 1
#Asks for user input
print("This is turn " + str(guessesTaken) + ". Try a code!")
guess = input()
#Easteregg codes
e1 = "1955"
e2 = "1980"
#Checks if only numbers have been inputted
if guess.isdigit() == False:
print("You can only use numbers, remember?")
guessesTaken = guessesTaken - 1
continue
#Checks whether guess is 4 numbers long
if len(guess) != len(code):
print("The code is only 4 numbers long! Try again!")
guessesTaken = guessesTaken - 1
continue
#Checks the code
if guess == code:
#In case the user guesses the code in 1 turn
if (guessesTaken) == 1:
print("Well done, " + name + "! You've hacked the code in " +
str(guessesTaken) + " turn!")
#In cases the user guesses the code in more than 1 turn
else:
print("Well done, " + name + "! You've hacked the code in " +
str(guessesTaken) + " turns!")
return
#Sets empty list for the feedback on the user inputted code
feedback = []
nodouble = []
#Iterates from 0 to 4
for i in range(4):
#Compares the items in the list to eachother
if guess[i] == code[i]:
#A match means the letter G is added to feedback
feedback.append("G")
nodouble.append(guess[i])
#Checks if the guess number is contained in the code
elif guess[i] in code:
#Makes sure the position of the numbers isn't the same
if guess[i] != code[i]:
if guess[i] not in nodouble:
#The letter is added to feedback[] if there's a match
feedback.append("C")
nodouble.append(guess[i])
#If the statements above are false, this is executed
elif guess[i] not in code:
#No match at all means an F is added to feedback[]
feedback.append("F")
nodouble.append(guess[i])
#Easteregg
if guess != code and guess == e1 or guess == e2:
print("Yeah!")
guessesTaken = guessesTaken - 1
else:
print(*feedback, sep=' ')
main()
You can try the game here:
https://repl.it/#optimusrobertus/Hack-The-Pincode
EDIT 2:
Here, you can see an example of what I mean.
Here is what I came up with. Let me know if it works.
from random import randint
class PinCodeGame(object):
def __init__(self):
self._attempt = 10
self._code = ['1231', '9997', '8829', '6765', '9114', '5673', '0103', '4370', '8301',
'1022']
self._easterEggs = ['1955', '1980', '1807', '0609']
def introduction(self):
print("Hi there stranger! What do I call you? ")
player_name = input()
return player_name
def show_game_rules(self):
print("10 turns. 4 numbers. The goal? Hack the pincode.")
print(
"For every number in the pincode you've come up with, I'll tell you whether it is correct AND correctly placed (G), correct but placed incorrectly (C) or just plain wrong (F)."
)
def tutorial_needed(self):
# Asks for tutorial
print("Do you want a tutorial? (yes / no)")
tutorial = input().lower()
# While loop for giving the tutorial
while tutorial != "no" or tutorial != "yes":
# Gives tutorial
if tutorial == "yes":
return True
# Skips tutorial
elif tutorial == "no":
return False
# Checks if the correct input has been given
else:
print("Please answer with either yes or no.")
tutorial = input()
def generate_code(self):
return self._code[randint(0, len(self._code))]
def is_valid_guess(self, guess):
return len(guess) == 4 and guess.isdigit()
def play(self, name):
attempts = 0
code = self.generate_code()
digits = [code.count(str(i)) for i in range(10)]
while attempts < self._attempt:
attempts += 1
print("Attempt #", attempts)
guess = input()
hints = ['F'] * 4
count_digits = [i for i in digits]
if self.is_valid_guess(guess):
if guess == code or guess in self._easterEggs:
print("Well done, " + name + "! You've hacked the code in " +
str(attempts) + " turn!")
return True, code
else:
for i, digit in enumerate(guess):
index = int(digit)
if count_digits[index] > 0 and code[i] == digit:
count_digits[index] -= 1
hints[i] = 'G'
elif count_digits[index] > 0:
count_digits[index] -= 1
hints[i] = 'C'
print(*hints, sep=' ')
else:
print("Invalid input, guess should be 4 digits long.")
attempts -= 1
return False, code
def main():
# initialise game
game = PinCodeGame()
player_name = game.introduction()
print("Hi, " + player_name)
if game.tutorial_needed():
game.show_game_rules()
while True:
result, code = game.play(player_name)
if result:
print(
"Oof. You've beaten me.... Do you want to be play again (and be beaten this time)? (yes / no)")
else:
print("Hahahahaha! You've lost! The correct code was " + code +
". Do you want to try again, and win this time? (yes / no)")
play_again = input().lower()
if play_again == "no":
return
main()
I am making a Dichotomous Key program where it asks questions to determine the name of the creature that is in question. Here is how it looks like right now:
step = 0
yes = ["y", "yes"]
no = ["n", "no"]
while step == 0:
q1 = input("Are the wings covered by an exoskeleton? (Y/N) ")
q1 = q1.lower()
if q1 in yes:
step += 1
elif q1 in no:
step += 2
else:
print("Huh?")
How would I put the if and else statement into a function so that I can reuse it for every question asked and change the step variable?
-Thanks
This is a working example:
step = 0
def update_step(q):
yes = ["y", "yes"]
no = ["n", "no"]
global step
if q in yes:
step += 1
elif q in no:
step += 2
else:
print("Huh?")
while step == 0:
q = input("Are the wings covered by an exoskeleton? (Y/N)")
update_step(q.lower())
print(step)
But i don't think it's a good way to solve the problem
UPDATE:
I like simplicity, that's why I try to get rid of state whenever I can. For example, I would write it this way:
total_steps = 0
def is_yes(answer):
return answer in ["y", "yes"]
def is_no(answer):
return answer in ["n", "no"]
def get_steps(answer):
if is_yes(answer):
return 1
elif is_no(answer):
return 2
return 0
while True:
answer = input('question? ')
steps = get_steps(answer.lower())
if steps == 0:
continue
total_steps += steps
break
print(total_steps)
you can make it better using more advanced techniques, but let's keep it simple :)
In the last line of my code. I am displaying the question, users answer and the correct answer. However, when i add in the answer section at the end it gives me an index out of range error. I can't seem to workout the issue. Can anyone help?
Thank you
import random
counter=0
score = 0
incorrect = 0
name=input("What is your name?")
print("Hi",name,",welcome to your math quiz!")
questions = ["10x2","4-2","6+12","6x4","12-5","6+54","1x0","3-6","4+0","65-9"]
answers=["20","2","18","24",'7','60','0','-3','4','56']
idx_questions = list(enumerate(questions))
idx_answers = list(enumerate(answers))
random.shuffle(idx_questions)
counter=0
inputs = []
for idxq, question in idx_questions:
print()
print("Question",counter+1,":",question)
print()
ans = input("What is the answer? ")
counter=counter+1
inputs.append(ans)
for idxa, answer in idx_answers:
if idxq == idxa and ans == answer:
print("Correct")
score=score+1
print("Correct Answers=",score)
print("Incorrect Answers=",incorrect)
elif idxq == idxa and ans != answer:
print("Incorrect. The answer is", answer)
incorrect=incorrect+1
print("Correct Answers=",score)
print("Incorrect Answers=",incorrect)
print("End of quiz")
print(name,"your score is",score,"out of 10")
print(score*10,"/100")
print(score*10,"%")
counter=0
while counter<10:
for idxq, question in idx_questions:
print("Question",counter+1,":",question,": Your answer =", inputs[counter],"Correct Answer =",answer)
counter=counter+1
Try it like this and don't worry about indexing, you can add flavor text where you intended:
from random import shuffle
questions = ["10x2","4-2","6+12","6x4","12-5","6+54","1x0","3-6","4+0","65-9"]
answers = ["20","2","18","24",'7','60','0','-3','4','56']
combo = dict(zip(questions, answers))
shuffle(questions)
score = 0
listing = []
for q in questions:
print(q)
ans = input()
if ans == combo[q]:
score += 1
listing.append((q, ans, combo[q]))
print(score / 10)
for item in listing:
print('For question {} you answerd {} and correct answer is {}'.format(*item))
I am trying to make a simple quiz program. But how do I get the input to be the same as the answer on the code where the user answers the question?
import random
print("what is your name \n")
name=input()
print("hello",name,"you will be asked 10 math questions goodluck")
for i in range(10):
op=["+","-","*"]
num1=random.randint(0,10)
num2=random.randint(0,12)
operation=random.choice(op)
eval(str(num1)+operation+str(num2))
print(num1,operation,num2)
while True:
try:
user= int(input())
if user=answer:
print("correct")
except:
print("invaild")
Avoid using eval, fix indendtation, address comments and you will get something like this.
import random
import operator
operators = {'+': operator.add,
'-': operator.sub,
'*': operator.mul}
print("What is your name?")
name = input()
print("Hello {name} you will be asked 10 math questions, good luck.".format(name=name))
for _ in range(10):
num1 = random.randint(0, 10)
num2 = random.randint(0, 12)
operator = random.choice(operators.keys())
answer = operators[operator](num1, num2)
print("{n1} {op} {n2} = ?".format(n1=num1, op=operator, n2=num2))
try:
user_input = int(input())
if user_input == answer:
print("Correct")
else:
print("Invaild")
except ValueError:
print('Please enter a number!')