Newbie to python and hit a snag in my latest program. Simply put, I’m trying to code up a decrement loop for a user input variable if possible. Essentially I have a global constant set to value e.g. 13, each time the program loops it prompts the user to input a value then that user value is shaved off 13 until it reaches 0. Problem is that it does shave it off but when it reiterates it resets the value to 13 and only removes the current iterate value entered. So if you enter 2 each iteration it just takes it down to 11… But I’m aiming for a result using 2 as an example again, 11, 8, 5, etc etc or using 3 as an example 10, 7, 4…. Any help guys will be much appreciated, cheers 🙂
a = 13
def main():
runLoop()
def runLoop():
while other_input_var > 0: # guys this is my main score accumulator
# variable and works fine just the one below
b=int(input('Please enter a number to remove from 13: '))
if b != 0:
shave(a, b)
def shave(a, b):
a -= b
print 'score is %d ' % a
if a == 0:
print "Win"
main()
Not an answer to your question, but rather a demonstration of string formatting. This is the old style, using the
%“string interpolation operator”.A session with this program:
The
%iin the original string is a placeholder for an integer (ifor integer) which is filled in later by the%operator on the string.There’s also
%ffor floating-point numbers,%sfor strings, and so on. You can do nifty things like specify how many decimal points numbers should print with –%.3ffor three decimal places – and so on.Another example:
This is a lot easier to read than:
Read up more about string formatting the old way or the new way.