Question

PYTHON Write a program to enter a valid variable till the user wants to end. The...

PYTHON

Write a program to enter a valid variable till the user wants to end. The program should display the count of positive, negative, zeros and letters (upper and lower case)/symbols (error handling) entered.

Based on the problem, you need to design an algorithm as follows:

1. Get the user input until “n” is entered

2. Add to the positive if it is greater than zero

3. Add to the negative if it is less than zero

4. Add to the zero if it is neither less than or greater than zero

5. Add to the letters/symbols if it none of the above

6. Output the totals as seen in sample output

Note: You need to prompt the user for input and continue to do so until “n” is selected and produce the specified output! (See below)

§ Hint: Use a Loop

Sample Output

User input: Enter input : 56

Do you want to Continue(y/n)? y

Enter input : -9

Output:

Positive Number(s) : 3

Negative Number(s) : 4

Zero(s) : 2 Letter(s) : 1

Symbol(s): 1

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

note: keep track of identation if any

#happy learning and don't forget to upvote

python code:

#!/usr/bin/env python
# coding: utf-8

# In[1]:


#create a dictionary to track count initialize the variables count to 0
count_dict={'greater_than_zero':0,'less_than_zero':0,'zero':0,'letter':0}


# In[2]:


#infinite loop
while True:
num =input("Input number or 'n' to quit : ")
if 'n'==num.lower(): #check for n, the breaking condition
break
try:
num=int(num)
if int(num) == 0:
#condition for zero
count_dict['zero']+=1
elif int(num) >0: #condition for positive numbers
count_dict['greater_than_zero']+=1
elif int(num) < 0: #condition for negative
count_dict['less_than_zero']+=1 #increment the counter
else:
continue
except:
count_dict['letter']+=1
continue
count_dict


# In[3]:


print('Positive Number(s) : {}'.format(count_dict['greater_than_zero']))
print('Negative Number(s) : {}'.format(count_dict['less_than_zero']))
print('Zero(s) : {}'.format(count_dict['zero']))
print('Symbol(s) : {}'.format(count_dict['letter']))


# In[ ]:


Add a comment
Know the answer?
Add Answer to:
PYTHON Write a program to enter a valid variable till the user wants to end. The...
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
  • 1. Write a program to add positive numbers. Ask the user to enter numbers greater than...

    1. Write a program to add positive numbers. Ask the user to enter numbers greater than zero. If a 0, is entered stop requesting numbers and display the total. If a number is entered that is less than 0, display the message ‘Number must be greater than 0’, then ask for another number to be entered. Repeat until a number greater than 0 or 0 is entered. use math lab so I can copy the answers

  • QUESTION 12 Write a program that would prompt the user to enter an integer. The program...

    QUESTION 12 Write a program that would prompt the user to enter an integer. The program then would displays a table of squares and cubes from 1 to the value entered by the user. The program should prompt the user to continue if they wish. Name your class NumberPowers.java, add header and sample output as block comments and uploaded it to this link. Use these formulas for calculating squares and cubes are: square = x * x cube = x...

  • C++ Write a program that prompts the user to enter integers or a sentinel to stop....

    C++ Write a program that prompts the user to enter integers or a sentinel to stop. The program prints the highest and lowest numbers entered, the sum and the average. DO NOT USE ARRAYS, only variables and loops. Write a program the prompts the user to input a positive integer. Keep asking the user until a positive integer is entered. Once a positive integer is entered, print a message if the integer is prime or not. (NOTE: A number is...

  • In python 3, 1. Write a program that takes a number of credit hours and returns...

    In python 3, 1. Write a program that takes a number of credit hours and returns a classification as follows: 29 credits or less: freshman; 30-59 credits: sophomore; 60-89 credits: junior; 90+ credits: senior. 2. Use a loop to valid input such that users can only enter positive numbers. 3. Use a loop to allow the user to keep entering credit hours and receiving classifications until they quit. All code must be in a function. I suggest the following structure:...

  • in Python: Write a program that allows the user to enter a number between 1-5 and...

    in Python: Write a program that allows the user to enter a number between 1-5 and returns an animal fact based on what the user entered. The program should 1. Continue to run until the user enters “quit” to terminate the program, and 2. Validate the user’s input (the only acceptable input is 1, 2, 3, 4, 5 or “quit”). You can use the following animal facts: An octopus is a highly intelligent invertebrate and a master of disguise. Elephants...

  • Create a program in Python that will allow the user to enter a temperature in Fahrenheit...

    Create a program in Python that will allow the user to enter a temperature in Fahrenheit which will then be converted to degrees Celsius. The program will keep asking the user for a Fahrenheit temperature until the user enters Q to quit. After each conversion the program needs to print out the degrees Celsius. The input prompts for this problem need to look like the following: Degrees Fahrenheit: Continue: For these input prompts watch the capitalization and spacing. There are...

  • In this exercise, write a complete Java program that reads integer numbers from the user until...

    In this exercise, write a complete Java program that reads integer numbers from the user until a negative value is entered. It should then output the average of the numbers, not including the negative number. If no non-negative values are entered, the program should issue an error message. You are required to use a do-while loop to solve this problem. Solutions that do not use a do-while loop will be given a zero. Make sure to add appropriate comments to...

  • Write a C++ program that prompts a user to enter a temperature in Fahrenheit as a...

    Write a C++ program that prompts a user to enter a temperature in Fahrenheit as a real number. After the temperature is entered display the temperature in Celsius. Your program will then prompt the user if they want to continue. If the character n or N is entered the program will stop; otherwise, the program will continue. After your program stops accepting temperatures display the average of all the temperatures entered in both Fahrenheit and Celsius. Be sure to use...

  • In C language using printf and scanf statements: Write a program to display a histogram based...

    In C language using printf and scanf statements: Write a program to display a histogram based on a number entered by the user. A histogram is a graphical representation of a number (in our case, using the asterisk character). On the same line after displaying the histogram, display the number. The entire program will repeat until the user enters zero or a negative number. Before the program ends, display "Bye...". The program will ask the user to enter a non-zero...

  • Write a program in python that asks the user to input a sentence. The program will...

    Write a program in python that asks the user to input a sentence. The program will ask the user what two letters are to be counted. You must use a “for” loop to go through the sentence & count how many times the chosen letter appears in the sentence. You are not allowed to use python built-in function "count()" or you'll get a Zero! Output will show the sentence, the letter, and the number of times the letter appears in...

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