I am quite new to python. One of the problems I've been having is using sys.stdin.readline(). I've written the code out like this con = sys.stdin.readline(). After doing this I put a if statement to look at that new data. After doing this no matter what I type the output is the exact same. Here is the code that I am using:
import sys
print('Nice and quick what\'s your name?')
name = sys.stdin.readline()
print('What a great name!')
def moon_weight(weight, gain):
for year in range(1,16):
weight = weight + gain
moon = weight * 0.165
if year == 15:
print()
print('Year %s: Your weight on the moon is %s' % (year, moon))
print()
print('That\'s the end of this program for you, %s' % name)
print('Have a nice day bye!')
else:
print()
print('Year %s: Your weight on the moon is %s' % (year, moon))
print()
print('Press enter to see the next year...')
print('If you would not like to see the next year type no')
nex = sys.stdin.readline()
if nex == 'no':
print()
print('Ok, ending program now.')
print('Ending...')
break
moon_weight(55,1)
When I run the code and type no, the code continues on like I didn't write anything.
sys.stdin.readline() returns the string "no\n"
Using the python interpreter interactively and testing sys.stdin.readline() == 'no' will return False.
You may try instead if "no" in sys.stdin.readline():
The problem is you are loooking for the EXACT string "no".
So even if using input() you may have problems if the user adds
spaces or types "No","No!", "NO" etc.
So you would have to normalize your string first.
if "no" in sys.stdin.readline().strip().lower():
could be a first simple approximation.
BTW. you can use underscores like next_ or _next if your variable
name clashes with keywords.
Use input instead.
import sys
print('Nice and quick what\'s your name?')
name = input()
print('What a great name!')
def moon_weight(weight, gain):
for year in range(1,16):
weight = weight + gain
moon = weight * 0.165
if year == 15:
print()
print('Year %s: Your weight on the moon is %s' % (year, moon))
print()
print('That\'s the end of this program for you, %s' % name)
print('Have a nice day bye!')
else:
print()
print('Year %s: Your weight on the moon is %s' % (year, moon))
print()
print('Press enter to see the next year...')
print('If you would not like to see the next year type no')
nex = input()
if nex == 'no':
print()
print('Ok, ending program now.')
print('Ending...')
break
moon_weight(55,1)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With