python - how would I put an if else statement in a function for abstraction? - python-3.x

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 :)

Related

Counting and Error Handling Block Guessing Game

Could you, please, help me to understand how should I use try/except block and count tries at the same time.
Here is the code without try/except block (it seems it's working fine):
import random
number = random.randint(1, 10)
tries = 3
name = input('Hi! What is your name?\n')
answer = input(f'{name}, let\'s play a game! Yes or No?\n')
if answer == 'Yes':
print(f'But be aware: you have only {tries} tries!\nReady?')
chat = input('')
print('Ok, guess a number from 1 to 10!')
while tries != 0:
choice = int(input('Your choice: '))
tries -= 1
if choice > number:
print('My number is less!')
elif choice < number:
print('My number is higher!')
else:
print('Wow! You won!')
break
print(f'You have {tries} tries left.')
if tries == 0 and choice != number:
print(f'Sorry, {name}, you lost... It was {number}. Try next time. Good luck!')
else:
print('No problem! Let\'s make it another time...')
This one is with try/except block.. Not sure where should I place 'choice' variable and where count 'tries', it keeps looping and looping:
import random
number = random.randint(1, 10)
tries = 3
name = input('Hi! What is your name?\n')
answer = input(f'{name}, let\'s play a game! Yes or No?\n')
if answer == 'Yes':
print(f'But be aware: you have only {tries} tries!\nReady?')
chat = input('')
print('Ok, guess a number from 1 to 10!')
while True:
try:
choice = int(input('Your choice: '))
if 0 < choice < 11:
while tries != 0:
tries -= 1
if choice > number:
print(f'My number is less!')
elif choice < number:
print(f'My number is higher!')
else:
print('Wow! You won!')
break
print(f'You have {tries} tries left.')
if tries == 0 and choice != number:
print(f'Sorry, {name}, you lost... It was {number}. Try next time. Good luck!')
else:
print(f'Hey {name}, I said, print a number from 1 to 10!')
except ValueError:
print('Please, enter a number!')
else:
print('No problem! Let\'s make it another time...')
Thanks!

Python multiply game. My script needs to have a arbitrarily number of players that each have to take turn in answering a multiplication question

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

Python number guessing game not displaying feedback correctly. What's the problem?

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()

Displaying answers from a list

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))

Python v3.3.5 maths questions program

Does anybody know why the score counter isn't working in this program? The program is designed to run 10 random maths questions and then display the score. However although it asks the questions, it will always display the score as being '0'
name = input('Type in your name')
questioncount = 0
score = 0
import random
for questioncount in range(0,10):
number1 = random.randrange(1,13)
number2 = random.randrange(1,13)
sign = random.randrange(1,4)
if (sign) == 1 :
print('{}x{}'.format(number1, number2))
elif (sign) == 2 :
print ('{}+{}'.format(number1, number2))
elif (sign) == 3 :
print ('{}-{}'.format(number1, number2))
answer = input('What is the answer?')
if (sign) == 1:
if (answer) == number1*number2:
score == score+1
else:
pass
if (sign) == 1:
if (answer) == number1+number2:
score == score+1
else:
pass
if (sign) == 1:
if (answer) == number1-number2:
score == score+1
else:
pass
pass
print('you got {} answers right!'.format(score))
There are some problems:
You forgot to change the numbers in the if statements.
You have to parse the input of the answer into int because input returns a string.
score == score+1 is a comparison and it always returns false. It should be "=". score = score + 1 or in a shorter way score += 1 is exactly the same
2 ifs nested for just one thing could be written in the same if statement.
You wrote this:
if (sign) == 2:
if (answer) == number1+number2:
score += 1
else:
pass
It could be:
if (sign) == 2 and (answer) == number1+number2:
score += 1
You should use if/elif statements instead of else:pass
This way
if (sign) == 1 and (answer) == number1*number2:
score += 1
elif (sign) == 2 and (answer) == number1+number2:
score += 1
elif (sign) == 3 and (answer) == number1-number2:
score += 1
and even better
if (sign == 1 and answer == number1 * number2) or (sign == 2 and answer == number1 + number2) or (sign == 3 and answer == number1 - number2) :
score += 1
That said, I would write this program like this:
import random
name = input('Type in your name\n')
score = 0
for _ in range(10):
number1 = random.randrange(1,13)
number2 = random.randrange(1,13)
sign = random.choice(["*", "+", "-"])
expression = "{0} {1} {2}".format(number1,sign,number2)
print(expression)
answer = int(input('What is the answer?\n'))
print("-------------")
if answer == eval(expression):
score += 1
print('{}, you got {} right answers!'.format(name, score))
Notes:
The use of _ it's because it is not important. You can call it whatever you want and you don't even have to declare it before.
The \n means carriage return.
You should do some more changes to prevent user from writting others characters in the answer, but I think you have work enough to do with this.

Resources