Python Forum
Correct number wrong position func.
Thread Rating:
  • 4 Vote(s) - 2.25 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Correct number wrong position func.
#1
import random
question = random.randint(1,1001)

def get_user_input():
    try:
        guess = int(input("Please enter a guess: "))
        return guess 
    except ValueError:
        print("Invalid guess! Please enter 3 digits between 0 - 9")
            
def get_hint(guess, question):
    if guess[i] != question[i]:
        print("W")
        ## print a 'w' for each number that doesn't match
    if guess[i] == guess[1]:
        #add to a dict? 
        # print a 'r' for each that does match
    if guess[1] == question[i-1]:
        #i want to check if the number is equal to another number in the guess, but not in the same matching position 
        # print 'x' for the right number in the wrong spot 
            
def play_game():
    print("Welcome to the RWX Game!")
    print("Your aim is to guess the target, consisting of 3 digits between 0 - 9")

    counts = 0
    remaining_score = 10000
    while remaining_score > 9000:
        x = get_user_input()
        if x != question:
            print("Your guess, %s, is incorrect!" %(x))
            get_hint(x, question)
            remaining_score = float(remaining_score - (remaining_score * 0.1))
            counts +=1
        else: 
            if x == question: 
                print("Congratulations!")
                print("%s was the correct answer" %(x))
                print("You guessed the correct answer in %d tries! Your score is %g!" %(counts, remaining_score))
    else:
        print("You Loose!")
        print("The correct answer was %s ! But you didn't guess it in 23 attempts!" %(question))
        again = str(input("Would you like to pay again? Y / N ?"))
        if again == 'y' or 'Y':
            play_game()
        else:
            print("Thank you for playing! Goodbye!")
            
play_game()
Hello! I have made a 'Guess the number game'. I am trying to write a get-hint(x,y): function that takes the users guess and the correct answers and compares them to each other, printing a specific letter if the numbers match, are wrong or match but are in the wrong position.

First issue: I'm not sure how to run through and match the numbers that are correct but in the wrong spot. Would it just be something like ? :

for i in guess:
    if i = question[i] 
Second issue:
I'm not sure how to get them to come together as one 3 digit hint.
I want them to print them like 'WXR' horizontally, not vertically. Should I using the results of the get_hint function by setting a variable or something ? and formatting them into a string with %?

If anyone could point me to the way I need to do this! I'm just stumped on how to make it work from here!

Thank you in advance !
Reply


Messages In This Thread
Correct number wrong position func. - by albry - Jan-10-2019, 10:01 AM
RE: Correct number wrong position func. - by albry - Jan-11-2019, 10:58 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to output one value per request of the CSV and print it in another func? Student44 3 1,280 Nov-11-2022, 10:45 PM
Last Post: snippsat
  Am I wrong or is Udemy wrong? String Slicing! Mavoz 3 2,388 Nov-05-2022, 11:33 AM
Last Post: Mavoz
  The code I have written removes the desired number of rows, but wrong rows Jdesi1983 0 1,603 Dec-08-2021, 04:42 AM
Last Post: Jdesi1983
  wrong forum, please delete. reposted in correct forum. woodmister 0 1,571 Jan-04-2021, 11:17 PM
Last Post: woodmister
  Func Animation not displaying my live graph jotalo 0 1,524 Nov-13-2020, 10:56 PM
Last Post: jotalo
  Trying to write func("abcd") -> "abbcccdddd" omm 8 3,984 Oct-24-2020, 03:41 AM
Last Post: bowlofred
  How can i judge 1st string position is correct number christing 3 2,369 Oct-30-2019, 03:32 AM
Last Post: newbieAuggie2019
  python gives wrong string length and wrong character thienson30 2 2,945 Oct-15-2019, 08:54 PM
Last Post: Gribouillis
  Number bug, calculation is wrong d3fi 2 2,430 Aug-27-2019, 05:29 AM
Last Post: perfringo
  call func from dict mcmxl22 3 2,822 Jun-21-2019, 05:20 AM
Last Post: snippsat

Forum Jump:

User Panel Messages

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