Control Flow issue

So I have been progressing a bit in my python 3 course,and thought I would go in to a workspace and mess around,and try to create a very small,simple Lotto style game,so that I could practice my skills and understanding of conditionals,and control flow,as well as various other concepts I have learned here at Codecademy and on other resources as well.

The problem I am having is that in my code,which I will share in a link to my workspace at the end,is that it is NOT throwing any errors,however the control flow does not seem to function correctly. Instead of executing code blocks where it should and when it should,every time regardless of if another code block should have been executed because a elif/and statement got met,it goes still directly to the else: statement and executes it on every occurance.

I am baffled by this,it seems to be a unexpected bug that I’m unsure of where to debug it at since there is no error to work with. It seems to be a matter of the fact the program is doing something it was not instructed to be doing. If anyone could please look a this,maybe run it a few times and be able to shed some light on what I might be missing I would greatly appreciate it.

https://www.codecademy.com/workspaces/617240d2bd2e4c6e5b29c8f1

Impossible. I haven’t looked at your code, but you can find the mistake in your logic the same way that I would if I had the time to do it. Start at the beginning, and follow the flow. Add print statements to print every value that you are sure should result in a condition being met that isn’t. You can see everything your code is doing by printing.

1 Like

Perhaps a example of what I’m seeing here might give some insight. In this picture I took using my phone on this test case I matched the zero,but not in order,however I know my elif statements cover this particular example yet it is still going directly to the else statement.

Hi rwcc2018.
I went through your code and did some investigating.
Your code works well, apart from when taking the users input:
This is because when using pythons builtin function input(), it returns the input as a string, however random.randint() returns an integer. These two cannot be compared as they are not the same data type.
The solution to this is casting the input from the user, to an integer,
like so:

#input from user integer_to_cast = "1" #showing the type print(type(integer_to_cast)) #by wrapping the value in int() #we can see that the type changes to int print(type(int(integer_to_cast)))

However, be careful when doing this! For this program, it is not an issue. But consider a case where the user does not input an integer, but instead types “a” for example. This will throw an error!
But for what you are doing here (learning about conditionals and control flows), it is somewhat beyond scope.

I hope this helps!
Happy coding!

Ah thanks,so do I have to cast every single digit 0-9? or can a blank “” suffice on this?

It’s just on the users input that you would need to cast (the random function you are using already gives you integers)

Like this:

user_num1 = int(input("First Number:")) user_num2 = int(input("Second Number:")) user_num3 = int(input("Third Number:"))

For clarity, here is your code with the integer casting added.

import random random_num1 = random.randint(0, 9) random_num2 = random.randint(0, 9) random_num3 = random.randint(0, 9) user_num1 = int(input("First Number:")) user_num2 = int(input("Second Number:")) user_num3 = int(input("Third Number:")) print(random_num1, random_num2, random_num3) print(user_num1, user_num2, user_num3) if user_num1 == random_num1 and user_num2 == random_num2 and user_num3 == random_num3: print("You win $10,000") elif user_num1 == random_num2 and user_num2 == random_num3 and user_num3 == random_num1: print("You win $1,000") elif user_num2 == random_num1 and user_num3 == random_num2 and user_num1 == random_num3: print("You win $500") elif user_num1 == random_num1 and user_num2 == random_num2: print("You win $50") elif user_num2 == random_num2 and user_num3 == random_num3: print("You win $50") elif user_num1 == random_num1 and user_num3 == random_num3: print("You win $50") elif user_num1 == random_num2 and user_num2 == random_num1: print("You win $15") elif user_num1 == random_num3 and user_num3 == random_num1: print("You win $15") elif user_num2 == random_num3 and user_num3 == random_num2: print("You win $15") elif user_num1 == random_num1: print("You win $5") elif user_num2 == random_num2: print("You win $5") elif user_num3 == random_num3: print("You win $5") elif user_num1 == random_num2: print("You win $1") elif user_num1 == random_num3: print("You win $1") elif user_num2 == random_num1: print("You win $1") elif user_num2 == random_num3: print("You win $1") elif user_num3 == random_num1: print("You win $1") elif user_num3 == random_num2: print("You win $1") else: print("Better luck next time")

Ok Thanks this will be easy to fix. Appreciate the help. I was quite baffled as to what was going wrong. Now it makes sense.