Question

Python My ITlab: 1. Write some code that repeatedly reads a value into the variable n...

Python My ITlab:

1. Write some code that repeatedly reads a value into the variable n until a number between 1 and 10 (inclusive) has been entered.

2. Write some code that repeatedly reads a value from standard input into the variable response until at last a Y or y or N or n has been entered.

3.Write a loop that reads positive integers from standard input, printing out those values that are greater than 100, each on a separate line, The loop terminates when it reads an integer that is not positive.

4.Write a loop that reads positive integers from standard input, printing out those values that are even, each on a separate line. The loop terminates when it reads an integer that is not positive.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Solution:

1Problem:

Code:

# this loop will iterate till n is in between 1 to 10 inclusive
while(True):
    # to except any conversion errors
    try:
        n = int(input(""))
        if(n>=1 and n<=10):
            break
    except:
        pass


Image:

SampleOutput:

2ndProblem:

Code:

# loop will iterate till response is equal to Y or y or N or n
while(True):
    try:
        response = input("")
        if(response == "Y" or response == "y" or response == "N" or response == "n"):
            break
    except:
        pass


SampleOutput:

3rdProblem:

Code:

# loop to read values till input value is negative
while(True):
    try:
        n = int(input(""))
        # if n is less than 0 break the loop
        if(n < 0):
            break
        # if n is greater than 100 print n
        if(n > 100):
            print(n)
    except:
        pass


SampleOutput:

4thProblem:

Code:

# loop will iterate till input value is negative
while(True):
    try:
        n = int(input(""))
        if(n < 0):
            break
        # print n if n is even number
        if(n%2 == 0):
            print(n)
    except:
        pass


SampleOutput:

Add a comment
Know the answer?
Add Answer to:
Python My ITlab: 1. Write some code that repeatedly reads a value into the variable n...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT