Python Forum
how to loop a script? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: how to loop a script? (/thread-34343.html)



how to loop a script? - ZYSIA - Jul-22-2021

Hi guys! I have create a python scripts to convert 3 random hexa number to decimal and add them together like below :
however, i want to try to loop it! keep running as long as the results is less than 3. any idea how to do that? i've tried while loop doesn't work..

import random
randomnumber = random.randint(0,99)
hex_number = str(hex(randomnumber))
hex_numberd0 = '0x' + hex_number[2:]
print('A random D0 :', hex_number)

randomnumber = random.randint(0,99)
hex_number = str(hex(randomnumber))
hex_numberd1 = '0x' + hex_number[2:]

print('A random D1 :', hex_number)

randomnumber = random.randint(0,99)
hex_number = str(hex(randomnumber))
hex_numberd2 = '0x' + hex_number[2:]

print('A random D2 :', hex_number)


D0 = hex_numberd0
D1 = hex_numberd1
D2 = hex_numberd2

#hexadecimal coversion
hex = D0
dec0 = int(hex,16)
print('Value in hexadecimal D0 :', hex)
print('Value in decimal D0 :', dec0)

hex = D1
dec1 = int(hex,16)
print('Value in hexadecimal D1 :', hex)
print('Value in decimal D1 :', dec1)

hex = D2
dec2 = int(hex,16)
print('Value in hexadecimal D2 :', hex)
print('Value in decimal D2 :', dec2)

#coversion ends

#inclination calculation
if(dec2 >> 7 == 1):
signbit = -1
else:
signbit = 1

Data0 = (dec0>>6)
Data1 = (dec1<<2)
Data2 = ((dec2&127)<<10)

print("D0 =", Data0)
print("D1 =", Data1)
print("D2 =", Data2)

DataI = signbit*(Data0 + Data1 + Data2)/1000
print("inclination = ", DataI)


RE: how to loop a script? - Gribouillis - Jul-22-2021

You could use a structure such as
result = 0
while result < 3:
    ...
    result = ...