if n = 1:
suit = hearts
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 pythonAttachment 960458
if generatedNumber == 20: suit = "clubs"
suits = ["hearts", "clubs", etc]
print ("suit: "+suits[random.randint(0,3)])
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.
So for your suits, you'd have a list such as:
suits = ["Hearts", "Clubs", "Diamonds", "Spades"]
Then to pick an element at random:
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).
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
Regarding your code that briefly appeared, see the edit I put on my post for why it wasn't working.
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
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"
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
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.
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.