0
Score Calculations on iterations in python
Getting Score on certain iterations results in python I coded a guessing game that generates a number (1 or 2) and the computer guesses the number I want to the code to run a certain number of times(10 in this case) and each time the computer guesses the number correctly, the score show increase by 5 Else, the score remains the same. I don’t know how to the iterations to output this result Can anyone pls help below is link to the code ##### https://code.sololearn.com/cihglokqzmP5/?ref=app
5 Respostas
+ 2
Bob_Li As always, pls don't just answer with the finished code. Let's help people to learn.
+ 1
Kunde David the main routine, which sets the score, has no idea of what happened in the guessing function. Make the function return the result, then use it to calculate the score.
BTW 1, randomnumber and computerguess are never uaed outside the function. Why do you set them in the beginning?
BTW 2, get used to put all functions on top and all root code in the bottom. A function in the middle of the code makes it less readable.
+ 1
Thanks Bob_Li i have seen my mistakes
All thanks to you
0
# Number Guess Game (computer)
import random as r
randomnumber = r.randint(1, 2)
computerguess = r.randint(1, 2)
def guessoutcome(n):
count = 0
for i in range(n):
print(f'\ntry {i+1}:')
randomnumber = r.randint(1, 2)
computerguess = r.randint(1, 2)
print(f'the random number generated is {randomnumber}')
print(f'computer guessed {computerguess}')
if computerguess == randomnumber:
count += 5
print('HURRAYYY!!!!! \nCOMPUTER GUESSED RIGHT')
print(f'computer earned {count} points')
else:
print('COMPUTER GUESSED WRONGLY\n')
#summary
print(f'\nTries:{i+1} Right:{count//5} Wrong:{i-count//5+1} Points:{count}')
#score calculator
tries = 15
guessoutcome(tries)
0
Emerson Prado
you are right. I was just unsure how to explain what changes need to be done to make the code work. Modifying the code was simpler.
About the code, I feel that the function is doing too many things.
If it was my code, it would be better to remove the function and write a procedural code instead. That would also solve the scoping problems.