Python Forum
QC input value against dictionary in while loop
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
QC input value against dictionary in while loop
#1
Hi, all. I'm super new to python and to OOP and was hoping for some help. I built a simple RPS game and wanted to include a check for user entry against a dictionary. If entry is invalid a simple print command asking for another entry and if the entry was valid, break and move onto the next line. The problem is when I tried within an existing loop, the break would skip additional steps. I couldn't figure it out so was hoping a kind stranger could help out this poor lost soul o_0.

My original code (without the horrible attempt at QC) is below. Could someone give me a hand as to how to have the choice input on line 51 QC'd against the user_legend dictionary on line 57? Thanks in advance!

#Version one with no QC check for rock, paper, scissor user entry
import sys
import random
import time
import math
#Ask for user name and greet user
user = input('Hi, what\'s your name? ')

#Intro yourself
print()
print('Hello,',user+'. I am RPS-Bot, and I am here to KILL you.')
time.sleep(1)
print()

print('Nah, I\'m just messin...')
time.sleep(1.5)
print('Seriously though, if you want to ',end="")
print ('live then play Rock, Paper, Scissors')

time.sleep(1.5)
print()
#print("Do you want to play until you're bored of a 'best of' match?")
#choice = input("Type 'U' for unlimited and 'B' for best of")
while 1==1:
    best = input('How many "best of games" do you want to play? ')
    try:
        int(best)
        break
    except ValueError:
        print(best,"??? I need a number. Humans, amirite?")
        print()
        continue
win_number = math.ceil(int(best)/2)
#Ask for user input for rock paper scissor. Can accept single letter or uppercase and lowercase full spelling

print()
print("Ok, best of",best+". First one to",str(win_number)+" wins!")
time.sleep(1)
print()
print("Pick Rock, Paper or Scissors \n"
               "(R, P, S if you're lazy.)")
time.sleep(0.5)
#Set Scores to Zero
rps_score = 0
user_score = 0


#start RPS loop
while (user_score < win_number and rps_score < win_number):
    print()
    choice = input("Whats your throw? ")

#generate RPS-bot throw
    rps = random.randint(1,3)
    rpslegend = {1:"Rock", 2:"Paper", 3:"Scissors"}
# create RPS legend
    userlegend = {'rock': 'Rock', 'r': 'Rock', 'paper': 'Paper', 'p': 'Paper', 's': 'Scissors', 'scissors': 'Scissors'}

# generate game possibilities
    possible_outcomes = {"Rock : Rock": "Tie, shoot again!", 'Rock : Scissors': "You win!",
                         'Rock : Paper': "You lose, sucka!",
                         "Paper : Paper": "Tie, shoot again!", 'Paper : Scissors': "You lose, sucka!",
                         'Paper : Rock': "You win!",
                         'Scissors : Scissors': "Tie, shoot again!", "Scissors : Rock": "You lose, sucka!",
                         "Scissors : Paper": "You win!"
                         }

    game = userlegend[choice]+' : '+rpslegend[rps]

#indicate winner
    print(game)
    time.sleep(0.25)
    print(possible_outcomes[game])

 #tally score
    if (possible_outcomes[game]) == "You win!":
        user_score += 1

    if (possible_outcomes[game]) == "You lose, sucka!":
        rps_score  += 1

#print score
    time.sleep(0.5)
    print(user+':',str(user_score)+'. RPS-Bot: ',str(rps_score))

#if tie, start loop over
    if (possible_outcomes[game])=='Tie, shoot again!':
        time.sleep(.5)
        continue

time.sleep(.75)
print()
if user_score == win_number:
    print('I lost??! NOW YOU MUST DIEEE!')
else:
    print('I won, RPS-Bot is the GREATEST OF ALL TIIIIIIIME!')
Reply
#2
Something like this simplified example
def get_input():
    user_input=""
    while user_input.lower() not in ["rock", "paper", "scissors", "r", "p", "s"]:    
        print()
        print("Pick Rock, Paper or Scissors \n"
               "(R, P, S if you're lazy.)")
        user_input=input()
    return user_input 
Reply
#3
Thanks for the reply -Sorry if I'm dense, where would I insert this? I tried in a few different cases and it wasn't working. Do I need to re-write the code with only one while loop using this defined function as the start of the loop or would I nest loops?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  WHILE LOOP NOT RETURNING USER INPUT AFTER ZerroDivisionError! HELP! ayodele_martins1 7 1,053 Oct-01-2023, 07:36 PM
Last Post: ayodele_martins1
  Code won't break While loop or go back to the input? MrKnd94 2 946 Oct-26-2022, 10:10 AM
Last Post: Larz60+
  WHILE Loop - constant variables NOT working with user input boundaries C0D3R 4 1,477 Apr-05-2022, 06:18 AM
Last Post: C0D3R
  For Loop and Use of Brackets to Modify Dictionary in Tic-Tac-Toe Game new_coder_231013 7 2,233 Dec-28-2021, 11:32 AM
Last Post: new_coder_231013
Exclamation question about input, while loop, then print jamie_01 5 2,649 Sep-30-2021, 12:46 PM
Last Post: Underscore
  Adding to the dictionary inside the for-loop - weird behaviour InputOutput007 5 2,693 Jan-21-2021, 02:21 PM
Last Post: InputOutput007
  Iterating over a dictionary in a for loop - checking code has worked sallyjc81 1 1,922 Dec-29-2020, 05:14 PM
Last Post: ndc85430
  I want to check if the input is str or is int & if it's str repeat the loop HLD202 4 2,780 Nov-23-2020, 11:01 PM
Last Post: perfringo
  Loop back through loop based on user input, keeping previous changes loop made? hbkpancakes 2 2,934 Nov-21-2020, 02:35 AM
Last Post: hbkpancakes
  How do you replace a dictionary key with a new input? thewetmosquito 4 3,300 Aug-23-2020, 03:48 AM
Last Post: perfringo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020