Horse racing simulator
Question:
I’m trying to make horse racing simulator, that makes strings add up randomly till they finish the max lenght, and i cannot seem to be able to do this correctly and i would like to have some assitance of what iam doing wrong with my currently provided code
import random
import os
from time import sleep
h1 = ''
h2 = ''
h3 = ''
h4 = ''
h5 = ''
while True: #len(h1) or len(h2) or len(h3) or len(h4) or len(h5) >= 15:
os.system('clear')
Horse_Select = random.choice((h1, h2, h3, h4, h5))
Horse_Select += '='
print(f'''{h1}
{h2}
{h3}
{h4}
{h5}''')
sleep(0.5)
#if len(h1) or len(h2) or len(h3) or len(h4) or len(h5) >= 15:
# break
#else:
# continue
Answers:
Variables don’t work like that. Put your horses in a list and choose a random index of the list.
import random
import os
from time import sleep
horses = ['', '', '', '', '']
while max(len(horse) for horse in horses) <= 15:
os.system('clear')
Horse_Select = random.choice(range(len(horses)))
horses[Horse_Select] += '='
print("n".join(horses))
sleep(0.5)
Spelled out to be closer to your code:
horses = ['', '', '']
while len(horses[0]) <= 15 and len(horses[1]) <= 15 and len(horses[2]) <= 15:
os.system('clear')
Horse_Select = random.choice([0, 1, 2])
horses[Horse_Select] += '='
print(horses[0])
print(horses[1])
print(horses[2])
sleep(0.5)
I’m trying to make horse racing simulator, that makes strings add up randomly till they finish the max lenght, and i cannot seem to be able to do this correctly and i would like to have some assitance of what iam doing wrong with my currently provided code
import random
import os
from time import sleep
h1 = ''
h2 = ''
h3 = ''
h4 = ''
h5 = ''
while True: #len(h1) or len(h2) or len(h3) or len(h4) or len(h5) >= 15:
os.system('clear')
Horse_Select = random.choice((h1, h2, h3, h4, h5))
Horse_Select += '='
print(f'''{h1}
{h2}
{h3}
{h4}
{h5}''')
sleep(0.5)
#if len(h1) or len(h2) or len(h3) or len(h4) or len(h5) >= 15:
# break
#else:
# continue
Variables don’t work like that. Put your horses in a list and choose a random index of the list.
import random
import os
from time import sleep
horses = ['', '', '', '', '']
while max(len(horse) for horse in horses) <= 15:
os.system('clear')
Horse_Select = random.choice(range(len(horses)))
horses[Horse_Select] += '='
print("n".join(horses))
sleep(0.5)
Spelled out to be closer to your code:
horses = ['', '', '']
while len(horses[0]) <= 15 and len(horses[1]) <= 15 and len(horses[2]) <= 15:
os.system('clear')
Horse_Select = random.choice([0, 1, 2])
horses[Horse_Select] += '='
print(horses[0])
print(horses[1])
print(horses[2])
sleep(0.5)