Jukku
Jukku

Reputation: 33

My seemingly simple integer comparisson doesn't work? Python

I'm trying to make a program that prints 8 options randomly with 7 of them being "Up" and 1 of them being "down". Then, the user is prompted to input the number that the "down" is displayed on. The only thing in my code that doesn't work is the comparison between the digit of the place that "Down" is displayed on and the user's input. Full code:

import random


def game_1():
    # original list
    up_down_lst = ["Up","Up","Up","Up","Up","Up","Up","Down"]

    # shuffles the list's items so they're randomized
    up_down_lst_shuffled = up_down_lst[:]
    random.shuffle(up_down_lst_shuffled)

    # just a print statement
    print("Pull up the lever that's down")

    # prints shuffled list items with numbers corresponding to its input number
    for i in range(8):
        print("[{}]".format(i + 1), up_down_lst_shuffled[i])

    # goes through the shuffled printed list again and checks for which number corresponds to "down"
    for j in range(len(up_down_lst_shuffled)):
        if "Down" in up_down_lst_shuffled[j]:

    # assigns the "down" number to a variable called down_num
            down_num = (j + 1)

    # asks for user input
    choice0 = input("> ")

    # compares uer input to see if it's the same as the "down" number
    if choice0 == down_num:
        print("Correct")
    else:
        print("Wrong, try again.")


# calls the function
game_1()

What's supposed to be output: (example)

Pull up the lever that's down
[1] Up
[2] Down
[3] Up
[4] Up
[5] Up
[6] Up
[7] Up
[8] Up
> 2
Correct

What is actually outputted: (example)

Pull up the lever that's down
[1] Up
[2] Down
[3] Up
[4] Up
[5] Up
[6] Up
[7] Up
[8] Up
> 2
Wrong, try again.

Upvotes: 1

Views: 46

Answers (2)

Anacktice Justice
Anacktice Justice

Reputation: 122

Hey dear in your code the input you are taking (choice0) is a string but the variable ‘down_num’ is an integer so thats why you are getting this error..

Try :

choice0 = int(input(“> “))

Upvotes: 0

Kuldeep Singh Sidhu
Kuldeep Singh Sidhu

Reputation: 3856

Your code is correct.

But input returns str

You are comparing int with str.

I would suggest you typecast the str that you get from input to an int

def game_1():
    # original list
    up_down_lst = ["Up","Up","Up","Up","Up","Up","Up","Down"]

    # shuffles the list's items so they're randomized
    up_down_lst_shuffled = up_down_lst[:]
    random.shuffle(up_down_lst_shuffled)

    # just a print statement
    print("Pull up the lever that's down")

    # prints shuffled list items with numbers corresponding to its input number
    for i in range(8):
        print("[{}]".format(i + 1), up_down_lst_shuffled[i])

    # goes through the shuffled printed list again and checks for which number corresponds to "down"
    for j in range(len(up_down_lst_shuffled)):
        if "Down" in up_down_lst_shuffled[j]:

    # assigns the "down" number to a variable called down_num
            down_num = (j + 1)

    # asks for user input
    choice0 = int(input("> ")) # type cast to int

    # compares uer input to see if it's the same as the "down" number
    if choice0 == down_num:
        print("Correct")
    else:
        print("Wrong, try again.")


# calls the function
game_1()
Pull up the lever that's down
[1] Up
[2] Up
[3] Up
[4] Up
[5] Up
[6] Up
[7] Up
[8] Down
> 8
Correct

Upvotes: 3

Related Questions