0

Basically i am using a random generator in python to generate a random number 3 times like this

for x in range(1,4):
    Card = random.randint(2, 14)
    print(Card)

However, now i want to compare the 3 random numbers that were generated to output a message stating the highest number that was generated

i.e if 4, 6 and 10 were randomly generated the message i want outputted would be "Your highest number is 10".

How would i attempt to do that i am stuck can someone please help?

martineau
  • 119,623
  • 25
  • 170
  • 301
TheProKi
  • 13
  • 1
  • 3
  • `cards = [random.randint(2, 14) for _ in range(1, 4)]` stores them in a list, so `cards[0]` is the first one, etc. – martineau Oct 25 '17 at 16:44
  • Possible duplicate of [Python - Find the greatest number in a list of numbers](https://stackoverflow.com/questions/3090175/python-find-the-greatest-number-in-a-list-of-numbers) – Aleks Andreev Oct 25 '17 at 16:50

4 Answers4

0

Use a variable to hold the highest card, and compare each to it.

highCard = 0
for x in rang(1, 4):
    Card = random.randint(2, 14)
    if Card > highCard:
        highCard = Card
    print(Card)
print("Your highest number is ", highCard)
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Hi, thanks for your answer it really helped, i'd like to just ask for one more thing. Lets say numbers 11-14 (As my range goes from 2-14) i wanted to output strings instead of values for example Jack, Queen, King & Ace how would i do that and how can i edit the code above to account for these strings in the highCard output value so for example if 11 was my highest number instead of saying your highest card is 11 i want it to say your highest card is Jack. – TheProKi Oct 25 '17 at 16:54
  • Use a dictionary that maps numbers to the names. – Barmar Oct 25 '17 at 19:10
0

You could do it with a list:

Numbers = []
for x in range(1, 4):
    Card = random.randint(2, 14)
    print(Card)
    Numbers.append(Card)

print(max(Numbers))

For your comment:

You can use dictionaries to do that

cards = {11:'Jack', 12: 'Queen', 13: 'King'}

print(cards[max(Numbers)])

This will print out 'Jack' if the number generated is 11.

WholesomeGhost
  • 1,101
  • 2
  • 17
  • 31
  • Hi, thanks for your answer it really helped, i'd like to just ask for one more thing. Lets say numbers 11-14 (As my range goes from 2-14) i wanted to output strings instead of values for example Jack, Queen, King & Ace how would i do that and how can i edit the code above to account for these strings in the highCard output value so for example if 11 was my highest number instead of saying your highest card is 11 i want it to say your highest card is Jack. – TheProKi Oct 25 '17 at 17:06
  • @TheProKi I editet my answer with how you can do that – WholesomeGhost Oct 25 '17 at 17:10
  • Thanks that works, however i come across an error when the number generated is neither 11, 12, 13 or 14? – TheProKi Oct 25 '17 at 17:22
  • well I didn't write the whole dictionary you can add the other elements by yourself – WholesomeGhost Oct 25 '17 at 17:34
0

Using a generator for storing the random.randint values, and then finding the max from it :

>>> rand = (random.randint(2,14) for i in range(3))
>>> max(list(rand))

One unfortunate side-effect of this is that you won't be able to access the rand generator once the max function is run on it.


Using list comprehension to store the values:

>>> rand = [random.randint(2,14) for i in range(3)]
>>> rand
=> [4, 11, 5]
>>> max(rand)
=> 11
Kaushik NP
  • 6,733
  • 9
  • 31
  • 60
-1

you can try:

max_Card = -5
for x in range(1,4): 
    Card = random.randint(2, 14) 
    if Card> max_Card :
        max_Card = Card
    print(Card)
S.Doe
  • 49
  • 1
  • 10