How can I get my Higher/Lower game to offer a new person at the end of the last?
Question:
I am trying to make it so that after one question asking the user which one they think is higher my program will determine if the answer is correct or incorrect and if it is correct then it will offer them that same person they just guessed correctly and then offer them a new random person from the data list. (Not sure if that makes sense but I was hoping someone could help as I’m now quite stuck!)
import random
from Games.Day9.game_data import data
from Games.Day9.art import logo
from Games.Day9.art import vs
# loop:
game_end = False
# score
score = 0
# random person 1:
random_person1 = random.choice(data)
# random person 2:
random_person2 = random.choice(data)
# making sure the player doesn't get two of the same people:
if random_person2 == random_person1:
random_person2 = random.choice(data)
"""Takes the account data and return a printable format of code."""
# formatting 1:
account_name1 = random_person1["name"]
account_followers1 = random_person1["follower_count"] # remove these from printer at first
account_description1 = random_person1["description"]
account_country1 = random_person1["country"]
# formatting2:
account_name2 = random_person2["name"]
account_followers2 = random_person2["follower_count"]
account_description2 = random_person2["description"]
account_country2 = random_person2["country"]
def start():
# higher or lower logo:
print(logo)
# where the first option goes:
print(f"Compare A: {account_name1}, a {account_description1}, from {account_country1}")
# vs sign:
print(vs)
# where the second option goes:
print(f"Against B: {account_name2}, a {account_description2}, from {account_country2}")
while not game_end:
def main():
# globals:
global score
global game_end
start()
print(f"Your current score is {score}")
# the users guess:
guess = input("Who has more followers? Type 'A' or 'B':n").upper()
if guess == "A":
if account_followers1 > account_followers2:
score += 1
print(f"You're correct! Your score is: {score}")
elif account_followers1 < account_followers2:
print(f"Sorry you were wrong. Your final score was {score}")
game_end = True
elif guess == "B":
if account_followers2 > account_followers1:
score += 1
print(f"You're correct! Your score is: {score}")
elif account_followers2 < account_followers1:
print(f"Sorry you were wrong. Your final score was {score}")
game_end = True
main()
Answers:
Your code structure looks a bit odd. Typically a ‘main’ function is called a single time, and inside that function you can have a loop. At the top of the file, define variables that will not change during execution. In the main function outside of the loop, define variables that should be maintained between loop iterations. Inside the loop, modify things as needed.
I’m assuming data
is a dict.
For example:
# Imports and Constants
import random
from Games.Day9.game_data import data
def main():
# Game Variables
score = 0
game_end = False
data: dict
while not game_end and len(data) >= 2:
# Pick 2 random elements from the dictionary and remove them
a = data.pop(random.choice(data.keys()))
b = data.pop(random.choice(data.keys()))
...
print("Game Over!")
if __name__ == "__main__":
main()
Also, note that the loop in my example would keep removing elements from the dict until you run out of choices, or decide to end the game otherwise. i think this is what you were asking for?
Try this, I have modified your code structure to work a bit better with a while
loop. This will continuously ask for a different unique person each time.
import random
from Games.Day9.game_data import data
from Games.Day9.art import logo
from Games.Day9.art import vs
# making sure the player doesn't get two of the same people:
def get_random_person(person_1, person_2):
original_person_2 = person_2
while person_1 == person_2 or original_person_2 == person_2:
person_2 = random.choice(data)
return [person_1, person_2]
def start(random_person1, random_person2):
"""Takes the account data and return a printable format of code."""
# formatting 1:
account_name1 = random_person1["name"]
account_followers1 = random_person1["follower_count"] # remove these from printer at first
account_description1 = random_person1["description"]
account_country1 = random_person1["country"]
# formatting2:
account_name2 = random_person2["name"]
account_followers2 = random_person2["follower_count"]
account_description2 = random_person2["description"]
account_country2 = random_person2["country"]
# higher or lower logo:
print(logo)
# where the first option goes:
print(f"Compare A: {account_name1}, a {account_description1}, from {account_country1}")
# vs sign:
print(vs)
# where the second option goes:
print(f"Against B: {account_name2}, a {account_description2}, from {account_country2}")
return account_followers1, account_followers2
def main():
score = 0
random_person1 = random.choice(data)
random_person2 = None
while True:
random_person1, random_person2 = get_random_person(random_person1, random_person2)
account_followers1, account_followers2 = start(random_person1, random_person2)
print(f"Your current score is {score}")
# the users guess:
guess = input("Who has more followers? Type 'A' or 'B':n").upper()
if guess == "A":
if account_followers1 > account_followers2:
score += 1
print(f"You're correct! Your score is: {score}")
elif account_followers1 < account_followers2:
print(f"Sorry you were wrong. Your final score was {score}")
break
elif guess == "B":
if account_followers2 > account_followers1:
score += 1
print(f"You're correct! Your score is: {score}")
random_person1 = random_person2
elif account_followers2 < account_followers1:
print(f"Sorry you were wrong. Your final score was {score}")
break
main()
Example output (with data I made myself):
Compare A: b, a bar, from gbr
Against B: c, a bat, from aus
Your current score is 0
Who has more followers? Type 'A' or 'B':
B
You're correct! Your score is: 1
Compare A: c, a bat, from aus
Against B: a, a foo, from usa
Your current score is 1
Who has more followers? Type 'A' or 'B':
A
You're correct! Your score is: 2
Compare A: c, a bat, from aus
Against B: b, a bar, from gbr
Your current score is 2
Who has more followers? Type 'A' or 'B':
A
You're correct! Your score is: 3
Compare A: c, a bat, from aus
Against B: a, a foo, from usa
Your current score is 3
Who has more followers? Type 'A' or 'B':
A
You're correct! Your score is: 4
Compare A: c, a bat, from aus
Against B: b, a bar, from gbr
Your current score is 4
Who has more followers? Type 'A' or 'B':
A
You're correct! Your score is: 5
Compare A: c, a bat, from aus
Against B: a, a foo, from usa
Your current score is 5
Who has more followers? Type 'A' or 'B':
A
You're correct! Your score is: 6
Compare A: c, a bat, from aus
Against B: b, a bar, from gbr
Your current score is 6
Who has more followers? Type 'A' or 'B':
A
You're correct! Your score is: 7
Compare A: c, a bat, from aus
Against B: a, a foo, from usa
Your current score is 7
Who has more followers? Type 'A' or 'B':
A
You're correct! Your score is: 8
Compare A: c, a bat, from aus
Against B: b, a bar, from gbr
Your current score is 8
Who has more followers? Type 'A' or 'B':
B
Sorry you were wrong. Your final score was 8
I am trying to make it so that after one question asking the user which one they think is higher my program will determine if the answer is correct or incorrect and if it is correct then it will offer them that same person they just guessed correctly and then offer them a new random person from the data list. (Not sure if that makes sense but I was hoping someone could help as I’m now quite stuck!)
import random
from Games.Day9.game_data import data
from Games.Day9.art import logo
from Games.Day9.art import vs
# loop:
game_end = False
# score
score = 0
# random person 1:
random_person1 = random.choice(data)
# random person 2:
random_person2 = random.choice(data)
# making sure the player doesn't get two of the same people:
if random_person2 == random_person1:
random_person2 = random.choice(data)
"""Takes the account data and return a printable format of code."""
# formatting 1:
account_name1 = random_person1["name"]
account_followers1 = random_person1["follower_count"] # remove these from printer at first
account_description1 = random_person1["description"]
account_country1 = random_person1["country"]
# formatting2:
account_name2 = random_person2["name"]
account_followers2 = random_person2["follower_count"]
account_description2 = random_person2["description"]
account_country2 = random_person2["country"]
def start():
# higher or lower logo:
print(logo)
# where the first option goes:
print(f"Compare A: {account_name1}, a {account_description1}, from {account_country1}")
# vs sign:
print(vs)
# where the second option goes:
print(f"Against B: {account_name2}, a {account_description2}, from {account_country2}")
while not game_end:
def main():
# globals:
global score
global game_end
start()
print(f"Your current score is {score}")
# the users guess:
guess = input("Who has more followers? Type 'A' or 'B':n").upper()
if guess == "A":
if account_followers1 > account_followers2:
score += 1
print(f"You're correct! Your score is: {score}")
elif account_followers1 < account_followers2:
print(f"Sorry you were wrong. Your final score was {score}")
game_end = True
elif guess == "B":
if account_followers2 > account_followers1:
score += 1
print(f"You're correct! Your score is: {score}")
elif account_followers2 < account_followers1:
print(f"Sorry you were wrong. Your final score was {score}")
game_end = True
main()
Your code structure looks a bit odd. Typically a ‘main’ function is called a single time, and inside that function you can have a loop. At the top of the file, define variables that will not change during execution. In the main function outside of the loop, define variables that should be maintained between loop iterations. Inside the loop, modify things as needed.
I’m assuming data
is a dict.
For example:
# Imports and Constants
import random
from Games.Day9.game_data import data
def main():
# Game Variables
score = 0
game_end = False
data: dict
while not game_end and len(data) >= 2:
# Pick 2 random elements from the dictionary and remove them
a = data.pop(random.choice(data.keys()))
b = data.pop(random.choice(data.keys()))
...
print("Game Over!")
if __name__ == "__main__":
main()
Also, note that the loop in my example would keep removing elements from the dict until you run out of choices, or decide to end the game otherwise. i think this is what you were asking for?
Try this, I have modified your code structure to work a bit better with a while
loop. This will continuously ask for a different unique person each time.
import random
from Games.Day9.game_data import data
from Games.Day9.art import logo
from Games.Day9.art import vs
# making sure the player doesn't get two of the same people:
def get_random_person(person_1, person_2):
original_person_2 = person_2
while person_1 == person_2 or original_person_2 == person_2:
person_2 = random.choice(data)
return [person_1, person_2]
def start(random_person1, random_person2):
"""Takes the account data and return a printable format of code."""
# formatting 1:
account_name1 = random_person1["name"]
account_followers1 = random_person1["follower_count"] # remove these from printer at first
account_description1 = random_person1["description"]
account_country1 = random_person1["country"]
# formatting2:
account_name2 = random_person2["name"]
account_followers2 = random_person2["follower_count"]
account_description2 = random_person2["description"]
account_country2 = random_person2["country"]
# higher or lower logo:
print(logo)
# where the first option goes:
print(f"Compare A: {account_name1}, a {account_description1}, from {account_country1}")
# vs sign:
print(vs)
# where the second option goes:
print(f"Against B: {account_name2}, a {account_description2}, from {account_country2}")
return account_followers1, account_followers2
def main():
score = 0
random_person1 = random.choice(data)
random_person2 = None
while True:
random_person1, random_person2 = get_random_person(random_person1, random_person2)
account_followers1, account_followers2 = start(random_person1, random_person2)
print(f"Your current score is {score}")
# the users guess:
guess = input("Who has more followers? Type 'A' or 'B':n").upper()
if guess == "A":
if account_followers1 > account_followers2:
score += 1
print(f"You're correct! Your score is: {score}")
elif account_followers1 < account_followers2:
print(f"Sorry you were wrong. Your final score was {score}")
break
elif guess == "B":
if account_followers2 > account_followers1:
score += 1
print(f"You're correct! Your score is: {score}")
random_person1 = random_person2
elif account_followers2 < account_followers1:
print(f"Sorry you were wrong. Your final score was {score}")
break
main()
Example output (with data I made myself):
Compare A: b, a bar, from gbr
Against B: c, a bat, from aus
Your current score is 0
Who has more followers? Type 'A' or 'B':
B
You're correct! Your score is: 1
Compare A: c, a bat, from aus
Against B: a, a foo, from usa
Your current score is 1
Who has more followers? Type 'A' or 'B':
A
You're correct! Your score is: 2
Compare A: c, a bat, from aus
Against B: b, a bar, from gbr
Your current score is 2
Who has more followers? Type 'A' or 'B':
A
You're correct! Your score is: 3
Compare A: c, a bat, from aus
Against B: a, a foo, from usa
Your current score is 3
Who has more followers? Type 'A' or 'B':
A
You're correct! Your score is: 4
Compare A: c, a bat, from aus
Against B: b, a bar, from gbr
Your current score is 4
Who has more followers? Type 'A' or 'B':
A
You're correct! Your score is: 5
Compare A: c, a bat, from aus
Against B: a, a foo, from usa
Your current score is 5
Who has more followers? Type 'A' or 'B':
A
You're correct! Your score is: 6
Compare A: c, a bat, from aus
Against B: b, a bar, from gbr
Your current score is 6
Who has more followers? Type 'A' or 'B':
A
You're correct! Your score is: 7
Compare A: c, a bat, from aus
Against B: a, a foo, from usa
Your current score is 7
Who has more followers? Type 'A' or 'B':
A
You're correct! Your score is: 8
Compare A: c, a bat, from aus
Against B: b, a bar, from gbr
Your current score is 8
Who has more followers? Type 'A' or 'B':
B
Sorry you were wrong. Your final score was 8