-1

This is an assignment for a computer science class. Is there a way to make it work, or do this more efficiently? The goal of the program is to have the user guess all four numbers (doesn't have to be in order), tell them if they are correct and display the amount of tries. The assignment requires at least one function with parameters and one list be included.

import random

# Sets tries and isCorrect to 0- not broken
tries = 0
isCorrect = 0

# Generates 4 random numbers- not broken
for i in range(4):
    code = random.randint(1, 9)
    code1 = random.randint(1, 9)
    code2 = random.randint(1, 9)
    code3 = random.randint(1, 9)
    
# Prints the random numbers for now- not broken
print(code)
print(code1)
print(code2)
print(code3)
 
# Guess = If the guess variables are equal to the code it tells you you're right
def guess(a, b, c, d):
    
    global isCorrect
    
    if a == code or code1 or code2 or code3 and b == code or code1 or code2 or code3 and c == code or code1 or code2 or code3 and d == code or code1 or code2 or code3:
        print("You got it correct!")
    else:
        print("You got it wrong.") 
        return(a, b, c, d)


# While isCorrect is still 0 it prompts you to guess again, if it's correct it sets isCorrect to one
while isCorrect == 0:
    gcode = int(input("Input your first guess: "))
    gcode1 = int(input("Input your second guess: "))
    gcode2 = int(input("Input your third guess: "))
    gcode3 = int(input("Input your fourth guess: "))
    guess(gcode, gcode1, gcode2, gcode3)
    tries = tries + 1
    #print("You got it wrong.")
    if gcode == code or code1 or code2 or code3 and gcode1 == code or code1 or code2 or code3 and gcode2 == code or code1 or code2 or code3 and gcode3 == code or code1 or code2 or code3:
        isCorrect = 1

# Makes it so the plural matches amount of tries
if tries != 1:
    print("It took you", tries, "tries.")
else:
    print("It took you", tries, "try.")
  • 1
    A downvote because the title does not match the code. Ensure that titles are accurate summaries of the actual issue presented. (The specific issue shown in the title -- `x == a or b or c` -- is a common Python mistake, yet as it doesn't actually appear in the code..) – user2864740 Dec 12 '21 at 20:37
  • If my question is badly written, can somebody explain why? – Birdsprout Dec 12 '21 at 20:39
  • Title: `.. gcode == code or code1 or code2 or code3 ..` -- how does this represent the code? – user2864740 Dec 12 '21 at 20:39
  • I was unsure if it would work or not. I will edit the post to include what I was asking about. – Birdsprout Dec 12 '21 at 20:40
  • As far as the currently shown code itself, consider this is *never* true given `b != c`: `a == b and a == c` (if `a` is equal to `b`, then `a` cannot equal `c`, because `b != c`; and thus both side of the `and` cannot be true at once). – user2864740 Dec 12 '21 at 20:41
  • As far as the title (which shows a questionable use of `or`), see https://stackoverflow.com/a/15112149/2864740 – user2864740 Dec 12 '21 at 20:44
  • And back the mismatch in the question, **Title**: "Why wouldn't `gcode == code or code1 or code2 or code3` work anywhere?". **Code**: `gcode == code and gcode1 == code1 and gcode2 == code2 and gcode3 == code3` -- these are clearly not the same. Ensure that the title and question are consistent and properly representative. – user2864740 Dec 12 '21 at 20:46
  • 1
    I've edited the post to better match the title. – Birdsprout Dec 12 '21 at 20:49
  • I've removed my downvote. See the linked question for how to write `x == a or b or c` in a way that will work .. reliably. (This is parsed as if it was written as `(x == a) or (b) or (c)`, which should more clearly show why it does not operate as expected.) – user2864740 Dec 12 '21 at 20:50
  • `if a == code or code1 or code2` is interpreted as `if (a == code) or (code1) or (code2)...`. Since code1 is a non-zero integer, it will be True, so the whole expression will always be true. You want `if a in (code, code1, code2, code3)...` – Tim Roberts Dec 12 '21 at 20:52
  • if (a == code) or (code1) or (code2) or (code3) – Birdsprout Dec 12 '21 at 20:56
  • Would this work better? – Birdsprout Dec 12 '21 at 20:56
  • No, `if (a == code) or (code1) or (code2) or (code3)` is exactly as `if a == code or code2 or code3` was written, which is the original problem. The parenthesis show *why* it is a problem (the `or` doesn't "read as English" and each clause is *independent*). See the description and solution(s) in the linked answer. – user2864740 Dec 12 '21 at 20:57
  • 1
    Does this answer your question? [How to test multiple variables against a single value?](https://stackoverflow.com/questions/15112125/how-to-test-multiple-variables-against-a-single-value) – Matthias Dec 12 '21 at 21:00
  • Not quite. I understand the answers but I'm not sure how to implement them into my program without having to make major changes. I'm not even sure how to make the changes needed. – Birdsprout Dec 12 '21 at 21:05
  • The code you wrote is invalid and there's no way to make it work, therefore you're going to have to make the changes. Maybe you should have asked the question sooner… – martineau Dec 12 '21 at 22:26

2 Answers2

0

I think you have to give your assignment right away therefore my help :-) I also added the use of lists.

Here is the working code but please double check it.

import random

# Sets tries and isCorrect to 0- not broken
tries = 0
isFalse = True # use boolean variable to make it more clear - 0 would be an integer


# Create a list in which you store the 4 random values
# Initialize the empty list, so it is defined in the for loop
random_values_list = []
# Generates 4 random numbers- not broken

for i in range(4):
    # append 4 times a random value to the list
    random_values_list.append(random.randint(1, 9))

# sort the list here is important, later we check the sorted input against this list
random_values_list.sort()
# Prints the random numbers
print(random_values_list)

 
# Guess = If the guess variables are equal to the code it tells you you're right
def check_guess(input_guesses_list, random_values_list):
    
    print(input_guesses_list)
    print(random_values_list)
    
    # now you can simply check if the lists are the same
    if input_guesses_list == random_values_list:
        print("You got it correct!")
        # return false to break the while loop, because the user got it right
        return False
    else:
        print("You got it wrong.")
        # return true to keep the while loop alive
        return True

# Save the inputs also in a list
input_guesses_list = []

# While isCorrect is still 0 it prompts you to guess again, if it's correct it sets isCorrect to one
while isFalse:
    input_guesses_list.append(int(input("Input your first guess: ")))
    input_guesses_list.append(int(input("Input your second guess: ")))
    input_guesses_list.append(int(input("Input your third guess: ")))
    input_guesses_list.append(int(input("Input your fourth guess: ")))
    # also sort this list
    input_guesses_list.sort()
    isFalse = check_guess(input_guesses_list, random_values_list)
    tries += 1
    # important: reset the list with the guesses!
    input_guesses_list = []
    

# Makes it so the plural matches amount of tries
if tries != 1:
    print("It took you", tries, "tries.")
else:
    print("It took you", tries, "try.")
  • This worked correctly., thank you. I will read over the code and try to understand it so I won't make mistakes in the future. – Birdsprout Dec 12 '21 at 21:20
-1

Your if statement is wrong. That's not the way to use or in python. this is the correct way:

if gcode == code or gcode == code1 or gcode == code2 or gcode == code3 and ...

The way you wrote it, it takes code1 (or code2, 3, ..) as an expression and checks if they are true. which is not what you would want here.

And of course, this is a messy way to code. One better way would be to put the codes in a list and gcodes in another list and then check them in a for loop.

Mohamad Bastin
  • 309
  • 2
  • 6
  • `if gcode in [code, code1, code2, code3, ...]` is easier to read and probably more Pythonic when there are 3 or more variables. – 9769953 Dec 12 '21 at 21:07