Question

In Python Exercise – For & While Loops Create a new file called loops.py and use...

In Python

Exercise – For & While Loops

Create a new file called loops.py and use it for all parts of this exercise. Remember the difference between input and raw input?

Be sure to test your code for each part before moving on to the next part.

  1. Write a program using a for loop that calculates exponentials. Your program should ask the user for a base base and an exponent exp, and calculate baseexp.
  2. Write a program using a while loop that asks the user to enter a number that is divisible by 2. Give the user a witty message if they enter something that is not divisible by 2-and make them enter a new number. Don’t let them stop until they enter an even number! Print a congratulatory message when they *finally* get it right.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

SOURCE CODE:

f=open("loops.py","a+") #open file in the append mode with loops.py as filename
print("Enter the base value:")
base=input()
print("Enter the Exponent value:")
exp=int(input())
f.write("base value is:"+str(base)+" and Exponent is:"+str(exp)+"\n") #writes the values of base and exponent into the file ,we cant concat int(base and exp) directly to string so we need to change by using str().
baseexp=1
for i in range(0,exp):
baseexp=baseexp*int(base) #multiplies the baseexp exp times as loop repeats exp times
f.write("The baseexp is:"+str(baseexp)+"\n") #writes result to the file
while 1: #util we break the loop it will repeats
print("Enter number divisible by 2")
num=int(input())
f.write("You Entered value is:"+str(num)+"\n") #writs to file
if num%2==0: #condition to check that the number is divisible by 2 or not
f.write("Congratulations you *finally* get it right"+"\n")
break;#if user enters the correct number the loop will break and program terminates
else:
f.write("Enter the number that should be divisible by 2"+"\n") #error message

#All the operations we performed are listed in the file

OUTPUT FILE:

main.py loops.py

base value is:2 and Exponent is:3

The baseexp is: 8

You Entered value is:

9

Enter the number that should be divisible by 2,

You Entered value is:7

Enter the number that should be divisible by 2

You Entered value is:

2

Congratulations you *finally* get it right 10

Add a comment
Know the answer?
Add Answer to:
In Python Exercise – For & While Loops Create a new file called loops.py and use...
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
  • Write a Python program using a for loop that calculates exponentials without using a Math module....

    Write a Python program using a for loop that calculates exponentials without using a Math module. Your program should ask the user for a base “base” and an exponent “exp”, and calculate baseexp.

  • While Loop - $100! 36 36 unread replies. 36 36 replies. Create a program that uses...

    While Loop - $100! 36 36 unread replies. 36 36 replies. Create a program that uses a while loop that asks the user to input $ amounts over and over until the the accumulated amount exceeds $100. At that point give a congratulatory message.

  • write inputs and outputs declare variables promt user b. Modify the program written for Exercise 9a...

    write inputs and outputs declare variables promt user b. Modify the program written for Exercise 9a to determine wener e eReTeO evenly divisible by a user-specified value, with no remainder. That is, is it evenly divisible by 3, 7, 13, or any other user-specified value? 10. (Data processing) As a part-time student, you took two courses last term. Write, run, and test a C++ program that calculates and displays your grade point average (GPA) for the term. Your program should...

  • Lab 5-2 Nested Loops 2. Summation Of Numbers (using Nested While Loops) Part A: The program...

    Lab 5-2 Nested Loops 2. Summation Of Numbers (using Nested While Loops) Part A: The program will calculate and display the total of all numbers up to a specific number (entered by the user). Only positive numbers are allowed. Part B: User-controlled loop Part A Input Validation loop Part A: Summation loop Examples: When 5 is entered the program will calculate the total as 1+2+...+5 and display 15. When 2 is enterered the program will calculate the total as 1+2...

  • python program 6 Write an input validation loop that asks the user to enter a number...

    python program 6 Write an input validation loop that asks the user to enter a number in the range of 100 through 1000? 7 Write an input validation loop that asks the user to enter ‘Y’, ‘y’, ‘N’, or ‘n’? 8 How many times the following loop will repeat? cnt = 0    while  cnt != 5: print(cnt) cnt = cnt + 2

  • For this interactive assignment, you will continue to utilize loops and functions and write a Python...

    For this interactive assignment, you will continue to utilize loops and functions and write a Python program that asks the user for an integer (number) greater than 1. Once the user enters an integer, it should then allow the user to CHOOSE between the following two options: If 1 is entered, a countdown from that number to zero is printed. If 2 is entered, the factorial of the number is printed. If the user inputs a number less than 1,...

  • Python 3.6 "While Loops" Write a program that adds up a series of numbers entered by...

    Python 3.6 "While Loops" Write a program that adds up a series of numbers entered by the user. The user should enter each number at the command prompt. The user will indicate that he or she is finished entering the number by entering the number 0.   Example This program will sum a series of numbers. Enter the next number (enter 0 when finished) > 5 Enter the next number (enter 0 when finished) > 2 Enter the next number (enter...

  • Starting out with Python 4th edition I need help with while loops I can't even get...

    Starting out with Python 4th edition I need help with while loops I can't even get one started correctly. Write a program a program that predicts the approximate size of a population of organisms. Problem 4.13 – population Use a while loop; there are no text boxes, just regular input statements Enter number of organisms: 2 Enter average daily increase: 30 Enter number of days to multiply: 10 DAY       APPROXIMATE POPULATION ------------------------------------------------- 1            2 2            2.600 3            3.380 4            4.394...

  • Use Python and only while loops For this part of the homework you will write code...

    Use Python and only while loops For this part of the homework you will write code to draw an isosceles right triangle. (A right triangle in which the height and base are the same size.) Your program should prompt the user for these inputs, in exactly this order: 1. The height of their triangle 2. The symbol the triangle will be outlined in 3. The symbol the triangle will be filled with For these inputs, you can assume the following:...

  • Please do this in C++ using only for loops, while loops, and do while loops. No...

    Please do this in C++ using only for loops, while loops, and do while loops. No arrays. Use for loop, while loop, and do while loops to generate a table of decimal numbers, as well as the binary, octal, and hexadecimal equivalents of the decimal numbers in the range 1-256 Note: See Appendix for number systems The program should print the results to the Console without using any string formats Design Details: The Main program should prompt the user for...

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