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...

  • In python create a program that does the following: Asks the user for their first name...

    In python create a program that does the following: Asks the user for their first name Asks the user for their last name Asks the user for the year they were born Calculates the age of the user from this number Gives an output message stating: It is nice to meet them (call them by name) And how impressed you are by their age

  • OBJECTIVES: To understand repetition structures To use while loops in a program To understand break and/or...

    OBJECTIVES: To understand repetition structures To use while loops in a program To understand break and/or continue instructions Write a Python program (with comments) to do the following: Create a variable count and initialize it to 0 Create a variable colors and initialize it to [] (i.e. an empty list) Ask the user if they would like to enter up to 3 favorite colors. They should enter ‘y’ or ‘n’. Assume they will enter a valid input. Save the response...

  • 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...

  • In C++ Programming, Try using loops only. This lab demonstrates the use of the While Loop...

    In C++ Programming, Try using loops only. This lab demonstrates the use of the While Loop and the Do While Loop as error checking mechanisms. You will be using each of these loops to solve the same problem. Please put them both in the same program. When you test the code, you will not see a difference in the way they execute - but there will be a difference in the logic when writing the code. You will want to...

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