Reputation: 31
Here is my code so far:
import sys
import time
import random
from sys import *
from random import *
from time import sleep
def intro():
global lives
lives = 2
intro1() def intro1():
global lives
text1 = 'please enter (Yes or No)'
for x in text1:
print (x, end='')
sys.stdout.flush()
sleep(0.007)
print()
answer = input()
if 'yes' in answer:
lives = lives + 1
text1 = 'Thank you'
text2 = '+ 1 life'
text3 = lives
text4 = ' lives now.'
for x in text1:
print (x, end='')
sys.stdout.flush()
sleep(0.007)
print()
for x in text2:
print (x, end='')
sys.stdout.flush()
sleep(0.007)
print()
for x in range(text3):
print (x, end='')
sys.stdout.flush()
sleep(0.007)
for x in text4:
print (x, end='')
sys.stdout.flush()
sleep(0.007)
print()
intro1()
if 'no' in answer:
lives = lives - 1
if lives <= 0:
text1 = 'Out of lives. Game Over.'
for x in text1:
print (x, end='')
sys.stdout.flush()
sleep(0.007)
end()
elif lives >= 0:
text1 = 'That\'s rude'
text2 = '- 1 life!'
text3 = lives
text4 = ' lives left'
for x in text1:
print (x, end='')
sys.stdout.flush()
sleep(0.007)
print ()
for x in text2:
print (x, end='')
sys.stdout.flush()
sleep(0.007)
print ()
for x in range(text3):
print (x, end='')
sys.stdout.flush()
sleep(0.007)
for x in text4:
print (x, end='')
sys.stdout.flush()
sleep(0.007)
print()
intro1()
else:
intro1() def end():
text1 = 'done.'
for x in text1:
print (x, end='')
sys.stdout.flush()
sleep(0.007) intro()
I know this looks really long, and confusing. The program runs fine I've tested it. My only problem is that it prints all the numbers.
Ex:
lives + 1 Thank you 0123 lives now.
or
Thats rude - 1 live 01 lives left.
I only want it to print the last number, but I want it to print with the animation I have. Help would be greatly appreciated.
Upvotes: 0
Views: 285
Reputation: 140
change range(text3)
to str(text3)
.
range(text3)
returns a list [0,1,2...text3-1]
. So that for loop is looping over that list of integers when you want to be looping over the characters in the string representation of the number. To fix that just change the integer, text3
, into a string and loop over the characters in the same way you are looping over the other strings.
Upvotes: 0
Reputation: 4118
You should change:
for x in range(text3):
print (x, end='')
sys.stdout.flush()
sleep(0.007)
to
for x in str(text3):
print (x, end='')
sys.stdout.flush()
sleep(0.007)
Note the difference, range(num)
will return a list of numbers, while str(num)
will convert it to an string from which you can print a character at a time.
BTW... Abiding by the DRY (don't repeat yourself) principle, you should consider factor-out the print to a single function,
def type_output(text):
for x in str(text): # Use str to coerce the input to a string
print (x, end='')
sys.stdout.flush()
sleep(0.007)
That way your code will become more readable.
Upvotes: 1
Reputation: 6826
I guess you don't like that your code is printing e.g. 0123? Where your code has, for example:
for x in range(text3):
print (x, end='')
sys.stdout.flush()
sleep(0.007)
what x in range(text3)
is doing is returning first 0, then 1, then successive numeric values up to 1 less than the integer value of text3. Look up the built-in range function in the python docs here: https://docs.python.org/3/library/functions.html
Instead, you should probably just have a single line of code:
print( text3, end='' )
this will convert text3 into a string and print it, and you won't get a leading zero :-)
Also where your code has e.g.
for x in text2:
print (x, end='')
sys.stdout.flush()
sleep(0.007)
What you are doing is printing the contents of text2 a character at a time. It would be much simpler to write:
print( text2, end='' )
although you may not need the end='' if your are next going to call print() to start a new line.
I don't know why you have the flush and short delay in there - I don't think they are needed, have never seen that used before. Ah, I guess that's the animation you are referring to. Have you considered making your coding life simpler by defining a function once, which does the animated printing, and simply calling that when needed?
def aniprint( stringtoprint ):
for c in stringtoprint:
print( c,end='' )
sys.stdout.flush()
sleep(0.007)
Then simply call it:
aniprint( text1 )
aniprint( text2 )
HTH Barny
Upvotes: 0
Reputation: 3689
When you call
for x in range(text3):
It will iterate over a "list" [0,1] when text3 = 2, [0,1,2] when text3 = 3 If you only want 3 then try:
for x in [3]:
or something similar, but you can just simply print is as well without the iteration.
Upvotes: 0