Python Help can somebody help me add trail count on this program
</>
’ ’ ’
def secret (c):
f= int(input(‘enter a number between 0 and 10:’))
while f:
c=c+1
if f<0 or f>10:
f=int(input('warning enter number between 0 and 10:'))
else:
if f==5 or f!=5:
if f==5:
print ('congrat')
print ('trial time,',(c))
return c
exit()
print('awesome')
elif f!=5:
a=5-f
if a in (-2,-1,2,1):
print ('you are too close')
else:
print('you are far')
secret(0)
What do you mean by trial count? I think what you want is creating a variable before the while loop and setting it to 0. Then at the start of the loop, increment the variable (kind of like what you do with c=c+1.
please make sure that you understand the code that you are entering … the logic of it. For example, the line above is always true, regardless of the value of f. For example, assume f is equal to any of the following:
f = 2 # not equal to 5, True
f = 5 # f is equal to 5, True
f = 24 # not equal to 5, True
f = 33 # not equal to 5, True
Note that the or means just that. If any of the conditionals in the conditional statement is true, then the whole statement is true.
A good development tool to use is to either put your script in debug mode so that you can step through it or print statements printing variable values after every assignment to follow along. Also, it is good practice to do this as you’re developing your code and not just after a big block of code.
thank for your reply actually i am new to coding . the code line you are referring to is actually for making output for each condition seperately and i refers below using if and else statement.
if f==5 or f!=5:
if f==5:
print ('congrat')
and
elif f!=5:
a=5-f
if a in (-2,-1,2,1):
print ('you are too close')
is there something wrong with the coding. please enlight me but as far as i know the program run good only the problem that i got is not understanding how to count trail time. thank you
Can you format you code more consistently? Readability is important when asking for help.
Help us help you. Read this link first and learn how to format code so we can help you better. About the Python Help category You will get more help this way.
And do you mean “trail” as in your message, or “trial” which is in your code? I don’t know which you want, or which might be a spelling error.
The recursive call to secret(0) (the one inside the function, not the one at the very end) is probably not what you want. If you’re trying to get it to prompt for another number, then move the f= int(input(...)) line inside of the loop, and get rid of the recursive call.
Since you have while f:, that means that if you enter 0 at the prompt, it will exit immediately, which is not what you want. You can change this to while True: instead, and the return statement that you already have will take care of exiting the loop.
if f==5:
# some action to do
elif f!=5:
# some other action to do
As is readily apparent, the first line with both conditional statements is ALWAYS true for ALL integers.
The two separate conditional statements below separate the condition when f is equal to 5 and when f is not equal to 5 (for all other values not being 5).
There are good tutorials online which you can readily google. I recommend working out a few worked out examples prior to striking it on your own.