#๐Ÿ”’ Picking a random object from a list

39 messages ยท Page 1 of 1 (latest)

north orbit
#

Very much a beginner but trying not to look up too many tutorials and problem solve myself...
So I tried to make rock paper scissors as my first 'proper project' and I'm stuck trying to get the computer to pick a random answer.
After a bit of googling I came across the 'randrange' function but I've never seen it and don't understand how it works:

print('Rock paper scissors')
choice = input('Choose: ')
comp_choice = ['rock', 'paper', 'scissors']
comp_choice.randrange(0, 2)

'rock' > 'scissors'
'scissors' > 'paper'
'paper' > 'rock'

if choice > comp_choice:
print('You win!')
else: print('You loose...')

Output after entering input for choice variable:
line 4, comp_choice.randrange(0, 2)
^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'list' object has no attribute 'randrange'

If anyone could give me a solution would be appreciated ๐Ÿ™‚ (also anyway to make the whole thing smaller/tidier if you're feeling extra nice) thanks

modern coyoteBOT
#

@north orbit

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

torpid geyser
#

!d random.choice

modern coyoteBOT
#

random.choice(seq)```
Return a random element from the non-empty sequence *seq*. If *seq* is empty, raises [`IndexError`](https://docs.python.org/3/library/exceptions.html#IndexError).
torpid geyser
#

!e
import random
print(random.choice('rps'))

modern coyoteBOT
#

@torpid geyser :white_check_mark: Your 3.12 eval job has completed with return code 0.

r
midnight gale
#

!res There are some good resources here.

modern coyoteBOT
#
Resources

The Resources page on our website contains a list of hand-selected learning resources that we regularly recommend to both beginners and experts.

north orbit
#

Ok thanks i'll have a look ๐Ÿ™‚

craggy igloo
#
'rock' > 'scissors'
'scissors' > 'paper'
'paper' > 'rock'

What are these lines meant to do?

midnight gale
#

better than learning-by-Google :)

craggy igloo
#

Or are those just placeholders?

north orbit
craggy igloo
north orbit
#

Ah ok yeah I see that now, so would I just make a bunch of if statements saying 'if comp choice = rock and choice equals paper

craggy igloo
#
'rock' > 'scissors'
'scissors' > 'paper'
'paper' > 'rock'

if choice > comp_choice:

It kind of looks like you're expecting Python to work like a declarative language.

#

Like prolog

north orbit
#

that is kind of what I was doiing yeah I was expecting it to save those comparisons

craggy igloo
#

Afaik, Prolog does though, so if you like writing code in a declarative style, maybe check it out.

#

Prolog is a logic programming language that has its origins in artificial intelligence, automated theorem proving and computational linguistics.
Prolog has its roots in first-order logic, a formal logic, and unlike many other programming languages, Prolog is intended primarily as a declarative programming language: the program is a set of facts ...

#

You state relationships, then have it infer things based on the declared relationships.

north orbit
#

never heard of it before i'll check it out ๐Ÿ™‚

#

I fixed the random issue I was having now just need to figure out the new problem you showed me ๐Ÿ˜…

modern coyoteBOT
#

Hey @north orbit!

It looks like you're trying to paste code into this channel.

Discord has support for Markdown, which allows you to post code with full syntax highlighting. Please use these whenever you paste code, as this helps improve the legibility and makes it easier for us to help you.

To do this, use the following method:
```py
print('Hello, world!')
```

This will result in the following:

print('Hello, world!')```
You can **edit your original message** to correct your code block.
north orbit
#
print('Rock paper scissors')
choice = input('Choose: ')
comp_list = ['rock', 'paper', 'scissors']

import random

comp_choice = random.choice(comp_list)
print(comp_choice)

#'rock' > 'scissors'
#'scissors' > 'paper'
#'paper' > 'rock'

#if choice > comp_choice:
#    print('You win!')
#else: print('You loose...')
craggy igloo
#

Figuring out the winner of a game of RPS is a fairly classic problem, and is a great problem-solving exercise. I wouldn't want to spoil the process.

#

If you run into errors/specific problems though, I'd be glad to help with those.

north orbit
#

Yeah thankyou for the help i'll go back to scatching my head now

north orbit
#
print('Rock paper scissors')
raw_choice = input('Choose: ')
choice = raw_choice.lower()
print(choice)
comp_list = ['rock', 'paper', 'scissors']

import random

comp_choice = random.choice(comp_list)
print(comp_choice)

if choice == 'rock' and comp_choice == 'scissors':
    print('You win!')
elif choice == 'scissors' and comp_choice == 'paper':
    print('You win!')
elif choice == 'paper' and comp_choice == 'rock':
    print('You win!')
elif choice == comp_choice:
    print('Draw!')
else: print('You loose...')
#

I'm sure there's a tidier way but it's done, thanks for the help

craggy igloo
#

Just a tip though: imports should always be at the very top.

north orbit
#

Oh ok cool thank you I actually had a note line I left out saying

#

# this is my first time learning about importing new functions like this

#

๐Ÿ˜…

craggy igloo
#

It's very common to see people import functions where they're first used.

If all imports are at the top though, it's very easy to find where names are being imported from. Imagine you have 10+ import statements importing all sorts of names. If they're scattered around your code, they're harder to find.

modern coyoteBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.