Bottom Page

• 4 Vote(s) - 2.25 Average
• 1
• 2
• 3
• 4
• 5
 Correct number wrong position func. albry Programmer named Tim Posts: 8 Threads: 4 Joined: Dec 2018 Reputation: 0 Likes received: 0 #1 Jan-10-2019, 10:01 AM ```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 ! Larz60+ aetate et sapientia Posts: 7,516 Threads: 287 Joined: Sep 2016 Reputation: 274 Likes received: 1041 #2 Jan-10-2019, 12:25 PM This can be greatly simplified, example: ```>>> list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0] >>> list2 = [4, 8, 2, 0, 5] >>> # same number, wrong postiion ... [b for a, b in zip(list1, list2) if b != a] [4, 8, 2, 0] >>> # same number same position ... [b for a, b in zip(list1, list2) if b == a] [5] >>> ``` You like this post perfringo Giant Foot Posts: 871 Threads: 1 Joined: Jun 2018 Reputation: 89 Likes received: 196 #3 Jan-10-2019, 12:33 PM Observations: - random.randint(1, 1001) can return values like 1, 10, 100 or 1000. If you ask user to enter 3 digits then it's quite hard to win if random returns 1, 10 or 1000. - you should be aware that function get_user_input() returns any value what can be converted to int. It will note enforce three digits rule. It can be a problem if you want give hints (user enters 123456 and correct number is 7, what hints there should be?) Regarding your question - keep in mind that int object is not subscriptable. I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy Life of Brian: Conjugate the verb, "to go" ! albry Programmer named Tim Posts: 8 Threads: 4 Joined: Dec 2018 Reputation: 0 Likes received: 0 #4 Jan-11-2019, 10:58 AM (Jan-10-2019, 12:25 PM)Larz60+ Wrote: This can be greatly simplified, example: ` >>> list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0] >>> list2 = [4, 8, 2, 0, 5] >>> # same number, wrong postiion ... [b for a, b in zip(list1, list2) if b != a] [4, 8, 2, 0] >>> # same number same position ... [b for a, b in zip(list1, list2) if b == a] [5] >>> ` Im Sorry, this is a little advanced for me. I understand that what you have included is running through and matching what I needed but I'm still not sure on how to make it work. Would I be setting list1 as my 'guess' variable and list2 as 'question' ? I've put it in like this: ```def get_hint(guess, question): list1 = str(guess) list2 = str(question) for a, b in zip(list1, list2): if b != a : print("X") else: for a, b in zip(list1, list2): if b == a: print("R") ```But all it is doing is printing off : X X X no what I enter, whether I put in a matching number, an incorrect number or a wrong position number. Is it not working because I've done with the 'for' and 'if' statement. Thank you for you help. I am still a baby at this and want to make sure I understand what I am doing, not just coping stuff! perfringo Giant Foot Posts: 871 Threads: 1 Joined: Jun 2018 Reputation: 89 Likes received: 196 #5 Jan-11-2019, 12:35 PM (This post was last modified: Jan-11-2019, 12:35 PM by perfringo. Edited 1 time in total.) Before adding new features to your code you should check whether existing code delivers expected results. It's unclear what are the rules of the game. I reiterate my observations: - 'question' value can be 1...1001 but you ask user to enter 3 digits. If random value is 1..99 or 1000-1001 it is not possible to win (FYI: random.randint: Return a random integer N such that a <= N <= b. Alias for randrange(a, b+1).) - your wording of message to user: "Your aim is to guess the target, consisting of 3 digits between 0 - 9" is misleading. You generate int with value in range 1...1001 as value of 'question' but then ask enter three digits in peculiar way. Why don't just say: please enter number in range 1...1001 (or maybe 100...999). In current wording 010 seems legit input. - you create 'question' value outside the game function. This means that if player chooses to continue playing the question value remains the same. I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy Life of Brian: Conjugate the verb, "to go" ! Larz60+ aetate et sapientia Posts: 7,516 Threads: 287 Joined: Sep 2016 Reputation: 274 Likes received: 1041 #6 Jan-11-2019, 04:01 PM Quote:Albry wrote: Im Sorry, this is a little advanced for me. I understand that what you have included is running through and matching what I needed but I'm still not sure on how to make it work. Would I be setting list1 as my 'guess' variable and list2 as 'question' ? I misunderstood the original question. I thought you were trying to show number of correct items in (guess history) list, as well as those in correct positions. the example I showed (although not relevant now that I understand the question) was if you had a random list and a guess history list as in a mastermind type of game, so example was: ``````Output: random_list: [1, 2, 3, 4, 5, 6, 7, 8, 9, 0] guesses: [4, 8, 2, 0, 5, ...] |____________ Correct Number, exact position `````` so all guesses are correct, but only one (5) is in the correct position, so ```def show_correct_guess(random_list, guesses, exact=True): if exact: return [b for a, b in zip(random_list, guesses) if b == a] else: return [b for a, b in zip(random_list, guesses) if b != a] ```would return [4, 8, 2, 0] if exact is False and [5] if exact is True You like this post « Next Oldest | Next Newest »

Top Page

 Possibly Related Threads... Thread Author Replies Views Last Post Number bug, calculation is wrong d3fi 2 159 Aug-27-2019, 05:29 AM Last Post: perfringo call func from dict mcmxl22 3 335 Jun-21-2019, 05:20 AM Last Post: snippsat About [from FILE import FUNC] Nwb 7 393 Apr-21-2019, 02:46 PM Last Post: snippsat Executing func() from a different folder ebolisa 2 362 Jan-14-2019, 10:18 AM Last Post: ebolisa How can I return my list from a func? Mike Ru 3 614 Oct-22-2018, 01:15 PM Last Post: buran [Regex] Findall returns wrong number of hits Winfried 8 688 Aug-23-2018, 02:21 PM Last Post: Winfried list func in lambda mepyyeti 1 971 Mar-10-2018, 09:07 PM Last Post: buran return variable in func mepyyeti 1 545 Mar-01-2018, 02:30 AM Last Post: Larz60+ How to get parent directory from existing func not user func ? harun2525 3 1,697 May-15-2017, 04:47 PM Last Post: harun2525

Forum Jump:

Users browsing this thread: 1 Guest(s)