3

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.

2
  • 2
    I could suggest instead of using sys.stdin.readline() to get a name from the user, use input('What is your name') for example, the same goes for the nex variable. Commented May 25, 2021 at 19:02
  • @PrimeBeat thank you! After changing that it works perfectly! Commented May 25, 2021 at 19:05

3 Answers 3

4

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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)

Comments

2

In addition to above comments, consider refactoring your code to only have one place for actual calculations:

def moon_weight(weight, gain, name, year_limit=16):
    for year in range(1, year_limit):
        weight += gain
        moon = weight * 0.165
        # This is the actual calculation. No need to repeat it twice
        print('Year %s: Your weight on the moon is %s.' % (year, moon))
        answer = input('Press enter to see the next year or type "no" to stop:')
        # This is the only branching logic for the quick stop
        if answer == 'no':
            print('Ok, ending program now.')
            print('Ending...')
            break
    else: # Special construct for the loop that runs if there was no break.
        print('That\'s the end of this program for you, %s' % name)
        print('Have a nice day bye!')


if __name__ == "__main__":
    name = input("What's your name?\n")
    moon_weight(55, 1, name, 5)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.