Posts: 45
Threads: 15
Joined: Oct 2017
Oct-28-2017, 12:03 AM
(This post was last modified: Oct-28-2017, 12:03 AM by atux_null.)
Hi. i have a homework to create a roulette where it has a few changes from the ordinary. The roulette is an endless loop which generates a number.
-If number is between 1-17 then it will print that the number is small
-If number is between 18-36 then it will print that the number is big
-if it is black or white
-if it is odd or even
the program runs until the user presses q to quit. otherwise it says next run and it generates a new number
here is my code and i need your help to make it work please, since i messed it a lot.
import random
red = [1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33,35]
black = [2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36]
green=[0]
number=random.randrange(0,37)
while color != 'q':
color = input("Please choose a color \nor press 'q' to stop the game: ")
color=input('Please choose a color: ')
if 1=>number<=17:
print('Number is small: ', number)
else:
if 18<=number<=36:
print('Number is big: ',number)
if number < 0 or number > 36:
print('Please play again')
else:
if number == 0:
print('Zero has been drawn from the roulette!')
elif (0 < number <= 10 and number % 2 == 1) or \
(11 <= number <= 18 and number % 2 == 0) or \
(19 <= number <= 28 and number % 2 == 1) or \
(29 <= number <= 36 and number % 2 == 0):
print('Number', number, 'is red.')
else:
print('Number', number, 'is black.') please for your help.
Posts: 606
Threads: 3
Joined: Nov 2016
Oct-28-2017, 06:26 AM
(This post was last modified: Oct-28-2017, 06:26 AM by heiner55.)
Maybe this helps:
#!/usr/bin/python3
import random
red = [1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33,35]
black = [2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36]
while True:
if input("press 'q' to stop the game or return to continue: ") == 'q':
break
number = random.randrange(0, 37)
print('\nNumber is: ', number)
if 1 <= number <= 17:
print('Number is small')
if number in red:
print('Number is red')
#if ....
#if ...
Posts: 45
Threads: 15
Joined: Oct 2017
Oct-28-2017, 05:59 PM
(This post was last modified: Oct-28-2017, 05:59 PM by atux_null.)
thanks a lot for the reply heiner55. Regarding the if statements. how can i make it more effecient instead of having multiple if? i have to check color, odd/even, small/big and all have to be printed in one sentence.
in case of number being zero then print 'Zero has been chosen.'
here is my attempt to create it, but it returns errors for spaces and intetation, but i cannot see where it is wrong.
import random
red = [1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33,35]
black = [2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36]
while True:
if input("Press enter to continue or 'q' to end the game: ") == 'q':
break
number = random.randrange(0, 37)
print('\nNumber is: ', number)
if 1 <= number <= 17:
print('Number is small')
else:
print('Number is big')
if number in red:
print('Number is red')
else:
print('number is black')
if number % 2 == 0:
print('Number is even ')
else:
print('Number is odd') is this correct? My other concern is how can i have all the answers in one line instead of multiple lines.
Posts: 1
Threads: 0
Joined: Oct 2017
I think the lists and some comparisons can be gotten rid of.
if number % 2 == 0:
print("Number is even and number is black")
else:
print("Number is odd and number is red")
Posts: 606
Threads: 3
Joined: Nov 2016
Oct-29-2017, 05:49 AM
(This post was last modified: Oct-29-2017, 05:50 AM by heiner55.)
txt = 'Number is: ' + str(number)
...
txt = txt + " and is red"
...
txt = txt + " and is green"
...
print(txt)
Posts: 45
Threads: 15
Joined: Oct 2017
Oct-29-2017, 08:59 AM
(This post was last modified: Oct-29-2017, 09:39 AM by atux_null.)
Thanks for your replies. regarding the spacing and indetation what am i doing wrong and it prints out errors in the aforemetnioned code?
please ignore my previous post. everything works fine.
Posts: 606
Threads: 3
Joined: Nov 2016
|