I have a infinite loop need to exit the script if condition meets and print my expected output
Code:
import random
stud=[0,0]
studnme = ['a','b']
while (stud[0] !=50 or stud[1] !=50):
if flag = 0:
lucky_draw = random.randint(1,6)
stud_marks = stud[0] + lucky_draw
stud[0] = stud[0] + lucky_draw
flag=1
else:
lucky_draw = random.randint(1,6)
stud_marks = stud[1] + lucky_draw
stud[0] = stud[1] + lucky_draw
flag=0
Output:
Student 'a' You have reached : 50
Student 'b' You have reached : 34
Need to print who as highest points
The values you're testing to exit the loop are incremented by random values, so they're not guaranteed to hit 50 exactly. Make the loop run while those values are less than 50.
while (stud[0] < 50 and stud[1] < 50):
import random
stud=[0,0]
studnme = ['a','b']
flag=0
while (stud[0] <=50 or stud[1] <=50):
if flag == 0:
lucky_draw = random.randint(1,6)
stud_marks = stud[0] + lucky_draw
stud[0] = stud[0] + lucky_draw
flag=1
else:
lucky_draw = random.randint(1,6)
stud_marks = stud[1] + lucky_draw
stud[1] = stud[1] + lucky_draw
flag=0
print(f'Student a you have reached :{stud[0]}')
print(f'Student b you have reached :{stud[1]}')
First of all you have not defined the flag, so set it to 0 or 1 and also the condition that you have kept should be a little changed, that is as any of the student has marks greater than 50 the program should terminate. Please check the above code.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.