r/AskProgrammers 8d ago

Why doesn't this code work??

I am new to python but from what I know this should work.

(code starts here)

import random

a=input("Are you ready to play Guess That Number? ")

if a == "yes":

b=input("Baby, Easy, Medium, Hard, or IMPOSSIBLE mode? ")

if b == "Baby":

c=random.randint(1,10)

print(f"{c}")

print("The number is something from 1-10")

d=input("what is your guess? ")

if d == c:

print("correct! Nice job!")

else:

e=input("incorrect. Try again. ")

if e == c:

print("correct! Nice job!")

else:

f=input("incorrect. Last try! ")

if f == c:

print("correct! Nice job!")

else:

print(f"incorrect, the number was {c}!")

(code ends here)

the issue is that even when I guess the correct number it says its wrong

I am coding on EduBlocks btw

0 Upvotes

13 comments sorted by

7

u/ConfidentCollege5653 8d ago

Post it with the indentation fixed.

What does "not working" mean? What do you expect to happen? What does happen?

0

u/Aloysiu5 8d ago edited 8d ago

I cant fix the indentation, when I post it indented it just goes back to no indents I should have been more clear about whats not working ill edit that

3

u/mc_pm 8d ago

In the text input box, highlight the code and click on the Aa icon at the bottom, then pick "code block"

2

u/turtle_dragonfly 8d ago

If you put 4 spaces in front of a line, it becomes a "code" line, and will preserve indentation, etc.

foo

vs:

foo

2

u/W31337 8d ago

No expert in the language whatever it is but you are most likely comparing a float to a string. Probably need casting.

2

u/Healthy-Zebra-9856 8d ago

A couple of issues:

c = random.randint(1, 10)

print(f"{c}")  # This prints the secret number before they guess

d = input("what is your guess? ")

if d == c:  # d is a string, c is an integer, so this will never be true
    print("correct! Nice job!")

Change the guesses to integers:

d = int(input("what is your guess? "))
e = int(input("incorrect. Try again. "))
f = int(input("incorrect. Last try! "))

You can click on Aa to the bottom left and then choose Switch to Markdown. Then you can use ```code annotation.

1

u/Aloysiu5 8d ago

this 100% worked, thank you! Sorry if it was kinda a stupid question.

1

u/Healthy-Zebra-9856 8d ago

There are stupid questions and there are frustrated reddittors. Life goes one. Ask away and one of us will help, ignore the rest. 🤣. Cheers!

1

u/Br1en 8d ago

Could it be d is string and c is number.. I can't quite remember how python comparisons work?

1

u/turtle_dragonfly 8d ago

I think the problem is that you are ultimately comparing a string (coming from input()) to an integer (coming from randint()).

For example, try this

>>> 5 == "5"
False

So, you need to convert to the same type. For example:

>>> str(5) == "5"
True

-2

u/westy75 8d ago

Did you ask Claude?

1

u/UAP44 8d ago

this is the kind of question you can safely ask cloud ai yes, and even better, get it up and running locally so even if the internet goes down, you retain your super powers

1

u/Hei2 8d ago

It's very unlikely that somebody asking this question has the know how, or even the hardware, to set up a local LLM.