lundi 20 mars 2023

Looping over a dictionary of random values... not pulling random values

Making a memory game for a class. Saved a dictionary of random shapes and colors for users to memorize. Each "round" is supposed to add on an additional shape. Wanted to give option to repeat a round, but I didn't realize if you loop over a dictionary with random.choice values, they would not be random every time. Is there a way around this? Or should I not give an option to play the same round...

def play_rounds(rounds_dict):
  user_name = intro()
  print()
  i = 1
  total_score = 0
  while i<=len(rounds_dict):
    round_score = 0
    print()
    print(f"Round {i}")
    print()
    time.sleep(1)
    for index, word in enumerate(rounds_dict[i]):
      if (index == 0) or (index % 2 == 0):
        make_shape(rounds_dict[i][index],rounds_dict[i][index+1])
    x=1
    for index, word in enumerate(rounds_dict[i]):
      if (index == 0) or (index % 2 == 0):
        added_score = user_input_check(rounds_dict[i][index],rounds_dict[i][index+1], place[x], round_score)
        x = x+1
        round_score = added_score
        print(f"Your current score is {added_score} out of {int(len(rounds_dict[i])/2)} in round {i}!")
    time.sleep(.5)
    total_score = total_score + round_score
    print()
    print(f"The correct sequence for Round {i} was: {rounds_dict[i]}")
    if added_score == len(rounds_dict[i])/2:
      print()
      print(f"Wow, {user_name}! You got them all right! That's impressive. If I were you, I would go to the next round. ")
    elif added_score > 0.5*(len(rounds_dict[i])/2) and added_score < len(rounds_dict[i])/2:
      print()
      print("Not too shabby, but you can do better!")
    elif added_score <= 0.5*(len(rounds_dict[i])/2):
      print()
      print(f"Don't be mad, {user_name}... but there's room for improvement... maybe you should try this round again!")
    # print(f"You have scored {total_score} points total!")
    while True:
      print()
      play_again = input("Do you want to play the same round, play the next round, or quit? Enter 'same', 'next', or 'quit'. > ")
      if play_again.lower() != "same" and play_again.lower() != "next" and play_again.lower() != "quit":
        print("That wasn't an option... please select the same round, the next round, or quit by entering 'same', 'next', or 'quit'.")
      elif play_again.lower() == "next":
        i = i+1
        break
      elif play_again.lower() == "same":
        break
      elif play_again.lower() == "quit":
        print(f"Goodbye {user_name}! Thanks for playing.")
        break
    if play_again.lower() == "quit":
      break

Tried restructuring and redoing loops. Not sure there is a way around this. Please help.




Aucun commentaire:

Enregistrer un commentaire