und zwar bereitet mir auf der Uni in Informatik 1 ohne Programmier Vorkenntnisse dieser Minitask ein Problem:
Write a function task_2_3. It should expect two parameters. The first is a limiter and the second is a target which is a positive integer with a value of 0 to 100. Randomly create numbers from 0 to 100 and count how long it takes until your random number is equal to the target. Repeat this process multiple times (defined by limiter param) and output the number of random values created in each run and return the average at the end.
Implement this as follows:
Create random numbers using the following code:
1. random_number = random.randint(0,100)
2. Count how long it takes until the random number is equal to the target (second parameter)
3. Escape this loop using break when you hit the target.
4. Output the number of values created until the target was hit.
5. Repeat this process multiple times (defined by the limit parameter)
6. Return the mean number of random values created across all runs.
Mein Code bis jetzt sieht so aus mit Kommentaren wie ich darauf komme:
Code: Alles auswählen
import random
def task_2_3(limiter,target):
target= range(0,100)
random_number = random.randint(0,100)
count = 0 #für das mitzählen wie lange die random zahl braucht um target zu erreichen
while True: #solange die random zahl nicht gleich target ist wird der counter um 1 erhöht
count += 1
if random_number == target: #ist die random number gleich target wird die schleife abgebrochen
break
print(random_number)
Vielen Dank im Vorraus!