The Student Room Group

Python

Py
(edited 3 years ago)
I can't see the output, but (if I've understood correctly) you'll probably want an if statement for that, i.e.

if n = 1:
suit = hearts

etc.
Original post by Selekt1234
so i needed help regarding this output message
so from the image you can see that it says "Round 1:10","Round3:Ace"
i have used random library to generate numbers between 2,14
and from 11,14 they would print specific words
However i also want to add the suits next to the number or character name using another number generator from 20,24 and each number from 20,24 would represent a suit so i set up a variable for it
However use print functions to put the together it does read the "words" for example for 20 it would be clubs but it just says 20
Btw i am new python
Attachment not found

As stated above, you'll want to run a check for the value generated. If you could show some of your code this would be even better. But I hope this helps.


if generatedNumber == 20:
suit = "clubs"
Although, perhaps a faster way of doing it would be with an array;

suits = ["hearts", "clubs", etc]
print ("suit: "+suits[random.randint(0,3)])
(edited 3 years ago)
So the way you're doing this currently adds a lot of unnecessary code. When choosing a card you need to check the value and print Ace/Jack/Queen/King if it's a certain value. You'd also need to map values to the suits.

A simpler way to approach this is to have arrays of valid values, then choose a random element from those arrays. This removes all the checks of "if number = 20, suit is hearts".

Effectively you'd have an array of cards, which contains "Ace, 2, 3, etc." up to King, and an array of suits which contain the four suits. Then you simply retrieve a random element from each array and print it.
Original post by Selekt1234
Can you show with a code example what you mean

I'm not going to write all the code for you, but I'll give you a rough idea and you can make it work from there.

So for your suits, you'd have a list such as:
suits = ["Hearts", "Clubs", "Diamonds", "Spades"]

Then to pick an element at random:
print(random.choice(suits))

random.choice() is a function which can be used to pick a random item from the list, so you don't need to mess around with picking a random number and then mapping it to a value. You can then create another list for the cards ["Ace", "1", "2", etc.] and randomly pick an entry in the same way. And of course if you want to store the values rather than printing them, just write to a variable as normal.

EDIT: Regarding the printscreen of your code (it appeared briefly but is gone now), check your variable names. You were setting the variable suit, but printing the variable suits. These are different variables (note the 's' at the end of suits being printed).
(edited 3 years ago)
Original post by Selekt1234
i know thats the other way which is much simpler but it has to be random number generated between any 3 numbers and each number corresponds to each suit

Is there a reason why you need to use random numbers? Because as you say the way I described is much easier. And if you need the numbers you can still calculate them. To add onto that, is there a reason you've chosen numbers arbitrarily? Why use 2-14 instead of 1-13? Why use 20-24 (note, 5 values here) for suits instead of 1-4?

Regarding your code that briefly appeared, see the edit I put on my post for why it wasn't working.
(edited 3 years ago)
Original post by Selekt1234
ahh yes random numbers is a the mandatory to show your suits for some reason
and the 20,23* is something i showed but yes it could 1-4
and also regarding my code

That is odd, but I'll assume it's a weird "do it this way because we told you to, rather than doing what's most effective".

Regarding your code, functionally what you are doing is more or less correct. Although the way you are choosing suits won't work. You're calculating the random number 4 times (once in each if statement) and then checking it. That's why you're getting weird output. Take the random number and put it in a variable, then yes if, elif to check it.

So something along the lines of:
rand_suit = random.randint(1,4)
if rand_suit == 1:
suit = "Hearts"
elif rand_suit == 2:
suit = "Spades"
etc. etc.

As far as the repeating nature of your rounds goes, I'm sure you can do that in a better way. If you find yourself repeating the same code over and over again, that implies a loop, a function or both may be useful. It's not entirely clear what you're trying to do, but I'd assume you can have some sort of loop which repeats however many times you need, picks the random card and adds the value to a rolling total.

Assuming your card values are 1 = Ace to 13 = King, you'd pick a random number from 1-13, add that value to the rolling total and print the associated card. To make mapping the cards simple, you could have an ordered list, then use the random value to index the list. So your code would be something like:

cards = ["Ace", "Two", ... , "King"]
suits = ["Spades", etc.]
score = 0
for i in range(1, 5):
random_card = random.randint(1, 13)
random_suit = random.randint(1, 4)
print("Round " + i + " : " + cards[random_card-1] + " " + suits[random_suit-1])
score += random_card

Here you're using i as a counter for each round, and choosing the card and suit using random numbers as required. The random_card value maps to the value of the card as ordered in the list, so it acts as both an index and a card value. It's used to update a rolling score, and the random value references the index of each list for the print statement. You could dump the main logic in a function, then call the function each time per player.

I'm reasonably sure that'll work if you filled out the list and added a print statement for the final score. You're output would be something like:

Round 1: Ace Spades
Round 2: Four Diamonds
Round 3: Two Spades
Round 4: King Hearts

Total score: 20

You can mess with the logic to get the output looking nicer, but I think that's roughly along the lines of what you're going for here
(edited 3 years ago)
Presumably you're trying to add the suit, which sometimes is an integer like "2", and sometimes a string like "King". In these scenarios you'd need to map the string back to a value, so "if King, add 13 to score". However this is redundant and totally unnecessary if the random numbers map to the cards themselves. If you're generating a random number from 1 to 13, where 1 = Ace and 13 = King, then you just add the random number. Because the random number is the value of the card.

This is effectively the same as what I described in my previous post, where you're using the random values to both choose a card and represent it's value.
I'm not really sure how else to explain it. You are generating a random number from 1 to 13. This number represents the chosen card and the score of that card. In other words, if your random number is a 5, the card is a 5 and it's worth 5. If the random number is 13, the card is King and it is worth 13.

You are not converting these numbers to strings. If you have some code that says "if random number is 13, card = King" you have two variables. The random number and the card. The random number is still a number, and the card is a string. But because the random number is the value of the card, you can add that to the score total. You print out the string stored in card, and add the random number you generated to the score. But this only works if the random numbers you are generating map to the card values.
Original post by Selekt1234
tried that and this is what i got:TypeError: can only concatenate str (not "int") to str

Looks like you're mixing up your variables. What's the code causing this?

Quick Reply

Latest

Trending

Trending