DEV Community

dongdiri
dongdiri

Posted on

DAY10: Python Day 13&14

Debugging
some debugging principles that I really need to follow:

Higher-Lower game

import art
from game_data import data
import random 
from replit import clear

#print higher lower logo 
print(art.logo)
#create variable for while loop 
right_answer = True 
current_score = 0

def check_answer(guess, A, B):
  if A > B:
    return guess == "A"
  else: 
    return guess == "B"

def compare(A, B):
  if A["follower_count"]>B["follower_count"]: 
    return A
  else: 
    return B

#pick random two dictionaires from list, store into variables 
A = data[random.randrange(0, 49)]
B = data[random.randrange(0,49)]
#create while loop 
while right_answer: 
  #print compare A: dictionary item 1, dictionary item 2, dictionary item 3
  print(f"Compare A: {A['name']}, {A['description']}, {A['country']}")
  #print vs art
  print(art.vs)
  #print against B: same thing 
  print(f"Against B: {B['name']}, {B['description']}, {B['country']}")
  answer = input("who has more followers? Type A or B: ")
  right_answer = check_answer(answer, A["follower_count"], B["follower_count"])

  clear() 
  print(art.logo)

  #if right answer: 
    #move answer to variable A 
    #pick new dictionary assign variable B
  if right_answer: 
    current_score += 1
    print(f"You're right! current score: {current_score}")
    A = compare(A, B)
    B = data[random.randrange(0, 49)]
print("wrong. Game over")
Enter fullscreen mode Exit fullscreen mode

Code works if there is data in a separate file called game_data.
I felt a strong necessity to get more familiar with creating functions and the return keyword to make the code more concise. I will utilize this more actively in later projects.

Top comments (0)