Ask your own question, for FREE!
Computer Science 7 Online
justahoomann:

anyone good at python???? how da hecc do i fix dis import comp_choice = random.randint (1, 3)

Gucchi:

Do you have a pic?

justahoomann:

uh yeah hang on

justahoomann:

SmokeyBrown:

I think you would not import "comp_choice" since that is the variable you are initiating. Instead, you would want to import the module you are using for the random number generation. Is that called "random"? Thanks for the picture too!

Gucchi:

maybe dont space out the =

Gucchi:

isnt it to spaced out?

SmokeyBrown:

So, before defining comp_choice, you would want a line that reads "import random" so that the module is recognized. Then, "comp_choice = random.randint(1,3)" should work

justahoomann:

@smokeybrown wrote:
So, before defining comp_choice, you would want a line that reads "import random" so that the module is recognized. Then, "comp_choice = random.randint(1,3)" should work
sorry that didnt work

SmokeyBrown:

Oh interesting. Did you try to run your code? And did an error message appear?

justahoomann:

yep

SmokeyBrown:

Oh how about this. Instead of random.randint(1,3) try random.randrange(1,3) Just like they show here https://www.w3schools.com/python/gloss_python_random_number.asp#:~:text=Python%20does%20not%20have%20a%20random%28%29%20function%20to,display%20a%20random%20number%20between%201%20and%209%3A

justahoomann:

TwT sorry for bothering you ik this is kinda annoying but it didnt work

SmokeyBrown:

Oh huh that is surprising! I'll do some more digging. In the meantime, do you think you could describe what happened when you tried to run your code?

justahoomann:

yeah

justahoomann:

justahoomann:

this^

SmokeyBrown:

It's saying 'random' is not defined, which is strange since it's supposed to be a module built into Python. Just making sure, did you make sure to import random at the top of your code? Also, since you're using randrange, you'd want to use (1,4) instead of (1,3), since it would make a range going up to the second parameter, not including it

SmokeyBrown:

What does your new code look like, by the way? That might help us find out what's going on

justahoomann:

justahoomann:

SmokeyBrown:

I'm not seeing that "import random" statement. Even though random is built-in to python, you still need to import it for the code to recognize it. Try putting "import random" at the beginning and see if the results change at all

justahoomann:

sorry wrong file here

justahoomann:

SmokeyBrown:

Ok, it looks like the import statement is in the middle of your while-loop, which shouldn't be the case. So, "import" just lets the code know that you want to use something from a specific library or module; you only need to do it once, usually at the beginning of the code before anything else. Also, you would import the module (or the parts of it that you need), not the statement you're trying to execute. Instead of "import random.randrange(1,3)" inside the while loop, move "import random" to the beginning of the code and your statement would look like comp_choice = random.randrange(1,4)

justahoomann:

apparently python hates me

SmokeyBrown:

Can you show me your current code and error message?

justahoomann:

SmokeyBrown:

I see. Make sure to put your "import random" statement at the top of the code, since it doesn't look like you have that there

justahoomann:

i did that like .2 seconds ago and it still didnt work

SmokeyBrown:

Interesting. Did the result change at all, even if it isn't what you wanted?

justahoomann:

nope

SmokeyBrown:

Still the same error message and everything, huh? How odd. Do you mind if I take a look?

SmokeyBrown:

(Sorry, off to have dinner now; I'll make sure to take a look when I'm back)

justahoomann:

sure ill put some more ss

justahoomann:

SmokeyBrown:

Thank you! The error message makes the issue quite clear: "IndentationError: unindent does not match any outer indentation level" On line 26, it looks like the code isn't lined up properly. Adding another space or two at the beginning of the line to make it match up should solve that problem. Python just happens to be one of those languages where indentation does matter, as you can see

SmokeyBrown:

Also, don't forget to change random.randrange(1,3) to random.randrange(1,4). A range of (1,3) would go up to 3, but not include 3, while a range of (1,4) would include 1, 2, and 3, but not 4.

justahoomann:

thx for helping with that problem but i now have another

justahoomann:

SmokeyBrown:

Glad to help! If you've got another problem, I don't mind taking a look at that one as well. Let's see...

SmokeyBrown:

Oh that one is simple! It looks like you named your variable "choice" earlier in the code, but on that line you called it "choice_name" by mistake, so it's not recognized.

justahoomann:

ah thanks

SmokeyBrown:

No prob. It often helps just to get another set of eyes on code. Working on these things for a long times tends to give us a form of "tunnel vision" XD

justahoomann:

this is my first time working with this so i appreciate it

SmokeyBrown:

No problem. It's true that I've had a little more experience, but everyone has had their first time at some point. Maybe before too long you'll be helping people out just like this :) Anyway, I'm glad we were able to figure it out!

justahoomann:

yeah thanks again for the help

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!