Advertisement
doanhtu

Choose random!

Apr 18th, 2018
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.56 KB | None | 0 0
  1. # Input a number. Random choice odd or even and compare with the input. If user choose correct, he win. If not, try again.
  2.  
  3. import random
  4.  
  5.  
  6. def odd_or_even():
  7.     n = int(input('Your guest: \n'))
  8.     random_choice = random.choice([(0, 'even'), (1, 'odd')])
  9.     print('We choose {}'.format(random_choice[1]))
  10.     if n % 2 == random_choice[0]:
  11.         print('You win')
  12.         return
  13.     else:
  14.         print('Try again\n------')
  15.         odd_or_even()
  16.  
  17.  
  18. Sample run:
  19.  
  20. Your guest:
  21. 2
  22. We choose odd
  23. Try again
  24. ------
  25. Your guest:
  26. 3
  27. We choose odd
  28. You win
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement