Write a program that repeatedly prompts a user for integer numbers

Write a program that repeatedly prompts a user for integer numbers

By using our site, you acknowledge that you have read and understand our Cookie PolicyPrivacy Policyand our Terms of Service. The dark mode beta is finally here. Change your preferences any time.

Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. I'm a beginner in python, and I'm creating a program that asks the user for as many numbers as they want until they input "stop".

Then the program would output how many numbers they entered and the result of all the numbers added together. Everything works well the first time, but as soon as I enter another number, the addition will be incorrect. You have a couple problems here. The first is that what you're trying to do is find the sum of all the numbers the user enters and print that out. But what you're really doing is only printing out the sum of the first number the user entered and the latest number.

Repeatedly asking for input

This line will throw an error if numberString is anything other than a number. If the user enters "stop", the program will instead throw an error because a string cannot be converted to a float. So before converting the numberString to a float, you should check to see if your condition has been met.

I am a beginner too in whole of programming. Sharing my learning here as first steps in using this website too. If the user enters anything other than a number, detect their mistake using try and except and print an error message and skip to the next number. Learn more. Program that asks user for numbers and outputs the sum and the amount of numbers entered Ask Question.

Asked 5 years, 5 months ago. Active 1 year, 6 months ago. Viewed 31k times. Active Oldest Votes. One of the books I am following for self-learning python has an exercise as follows. And thanks John, I tried it this way using your help.

Ash conference 2020

Anand Surampudi Anand Surampudi 2 2 silver badges 9 9 bronze badges.By using our site, you acknowledge that you have read and understand our Cookie PolicyPrivacy Policyand our Terms of Service. Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It only takes a minute to sign up. The function of this program is repeatedly prompting a user for integer numbers until the user enters 'done'.

Once 'done' is entered, print out the largest and smallest of the numbers. Suppose all input is proper, so we do not need to check whether the input value is illegal or not. It's a shame python doesn't have a do. You do plenty of branching.

For every valid input, you check if the extreme values so far are still None. If you think about it, this will only be the case the first time a valid value is entered. It's not very plausible to keep on performing that check and it needlessly bloats the code. To find maximum and minimum efficiently, use max and min and initialise your variables with appropriate valuesthat make the initial case blend in with the rest of the operation. Everything is bigger than -infinity, which makes it ideal as a starting value for the largest number.

Here's what your code would look like with those changes applied:.

write a program that repeatedly prompts a user for integer numbers

Good work on not just storing all the entered numbers in a list, and calling max and min on the final product. You could combine the code for setting smallest and largest into single conditions:. In general, you should always catch as specific an exception as possible — only the errors you expect to see.

Read PEP 8 ; no spaces before semicolons. I would be more tolerant of malformed user input — for example, if I enter doneor Doneor DoneI think the intention is pretty clear. You could call max on to variables directly and put the if statements in line and avoid the try catch by checking if the user input is digits.

My personal belief is that if you don't need the try catch, don't use it. However the above code is analogues to yours. Memory is not an issue. Fundamentally there is a great danger in over designing and in such cases, quick and dirty works best. Sign up to join this community. The best answers are voted up and rise to the top.

Lavoratori dello spettacolo decreto agosto

Home Questions Tags Users Unanswered. Prompt user for some numbers, then print the max and min Ask Question.By using our site, you acknowledge that you have read and understand our Cookie PolicyPrivacy Policyand our Terms of Service. The dark mode beta is finally here.

Change your preferences any time. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. I'm taking a class in Python and our prof wants us to write a program that prompts the user to enter an integer repeatedly until they enter 0. Then, have the program ignore all negative numbers, if any, and display the number of even integers, the number of odd integers, the sum of the even integers, the sum of the odd numbers, and the number of positive integers.

I've been trying and trying to do this program in small parts. However, I always end up getting stuck. I've started over about 5 times now and I would really appreciate if someone were to point me in the right direction.

I know it's not much and I'm missing a whole lot, but I just wrote what I know needs to be included so far.

write a program that repeatedly prompts a user for integer numbers

I keep getting stuck because the program keeps giving me errors. Any sort of help is very appreciated because I have really no idea where to start! What you were doing is trying to check for each character in the string. Just not what you'd want. It's probably your text editor's fault, so try to configure it to use 4 spaces instead, which is the standard in Python.

Learn more. Python program prompts user to enter number until they enter 0, then program adds even and odd integers Ask Question. Asked 2 years, 10 months ago. Active 3 months ago. Viewed 15k times. Pang 7, 16 16 gold badges 70 70 silver badges bronze badges.

Nora Nora 17 1 1 gold badge 2 2 silver badges 6 6 bronze badges.By using our site, you acknowledge that you have read and understand our Cookie PolicyPrivacy Policyand our Terms of Service.

Subscribe to RSS

The dark mode beta is finally here. Change your preferences any time. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. I was trying to figure out what statement to use to get the user to enter a number between 1 and Number beteween 1 to 10 right? Learn more. How to prompt the user to enter a integer within a certain amount of numbers Ask Question.

Asked 6 years, 1 month ago. Active 2 years, 4 months ago. Viewed 17k times. You've inadvertently answered your own question with your tag - use a "while-loop".

And the problem of this is? Active Oldest Votes. David Ranieri Emu Emu 4, 1 1 gold badge 22 22 silver badges 41 41 bronze badges. This is a pretty poor design. It would be better to loop while the input is invalid and then handle the processing after that.

Also means that you can get rid of the artificial loop condition and loop on input instead. As a side note, the presence of continue in C code is almost always a certain indication of poor design. Jayesh Bhoi Jayesh Bhoi 17k 10 10 gold badges 48 48 silver badges 64 64 bronze badges.

Why not use the do. Sergiu Sergiu 2 2 silver badges 14 14 bronze badges. Sign up or log in Sign up using Google.

Banyak omong tts

Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog. Q2 Community Roadmap. The Unfriendly Robot: Automatically flagging unwelcoming comments. Featured on Meta. Community and Moderator guidelines for escalating issues via new response….

Feedback on Q2 Community Roadmap.By using our site, you acknowledge that you have read and understand our Cookie PolicyPrivacy Policyand our Terms of Service. The dark mode beta is finally here. Change your preferences any time. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. There must be an easier way to do this than to make a variable for each number. The code is not complete. I haven't even started on the two other modules yet.

Thanks in advance.

Xenogen ivis imaging system

You can knock out all three function requirements by encapsulating each step of your program. Rather than having your loop inside of a function, we'll let main control the loop, and we'll control the flow by passing data into and out of function calls. The next function we'll set up is straight from your list of requirements. From all of the numbers that the user enters, we need to find the biggest one.

write a program that repeatedly prompts a user for integer numbers

From the language alone, we can determine that we're looking through a set of numbers, and thus, this is a good place to break out a list. Assuming we store all of the users input in a list, we can then pass that list to a function and perform operations on it, like so. We set the first element of the list to a variable highest and then check all other elements in the list against that initial value. If we find one that's bigger, we then reassign the highest variable to the element that was bigger.

Once we do this for each element in the list, the number inside of highest will now be, just that, the highest number, and so, we'll return it to the main program. Finally, we get to our main loop. And we use the sum of that as our loop condition. Once the sum of the numbers in the list exceedthe while loop will exit and we can finally show the user the results. They'll loop though the lists, and then return the appropriate values to the print statements.

Yes, of course there's a better way than making a variable for each number. Store them in a list. Storing them in a list also makes finding their sum and the highest and lowest value easy there are built-in functions for this. As a further hint, you'll want to use two loops, one inside the other. The outer loop keeps the user entering numbers until their sum exceeds The inner loop keeps the user entering a single number until it's between 1 and 42 inclusive.

And as others already said, it is a good idea to create a list and append a new element to it each time the user enters a valid number until the sum of the numbers axceeds Thanks to everyone who answered.

I've written the following code and it works nearly perfectly except the last module. If you exit the program after the first run by pressing 0 it will exit, but if you run it a second time pressing 0 won't make it exit.

It'll just go back to the beginning of the program. I don't know why. Note: The assignment has various requirements that I had to incorporate in my code.

Hence the unnecessary modules, global variables, and comments.Hi there, I'm writing my first Python script for a n imperative programming class at university, and I want to make it a little better.

It's meant to be fairly short, 50 lines.

Subscribe to RSS

I've written the basic 'mechanism' to look at the user input. Instead of simply printing every property of the input it analyses an integer input and tells you if they are such and such, WOW! Fair enough. I also want the program to repeatedly ask the user for input to type in one of the keys from the dictionaryso each time there is an input, it prints the value and then asks the same question. This is my main issue. I've only seen the Python tutorial, and I haven't found anything relevant.

Katanungan tungkol sa wikang filipino

Use while loops, if you've got the Zelle book read that, if not I suggest you get it. Click here. While loops, if set to "True" basically means they will continuously cycle through the loop forever, unless you enter a "break" function within the loop's body if a certain condition has been met.

Until the user has entered a word, and not a number, the while loop will continuously run around the body i. Causing the question to stop being asked. Note: you can enter conditions in the while loop rather than just Truebut like I say, get the Zelle book and read it - I'm a student and it's helped me out a massive amount.

I'm not quite sure I follow you, sorry. I've only used 'while' once, and it was to decide whether or not to ask the user for input a second time.

The loop you gave seems to be concerned whether the input is a word. I don't understand your loop entirely or how I could integrate it into my script, sorry. Maybe I should post parts of my code so far end of post? My question is badly worded, I'll admit. English isn't my first language, and my mastery of Python or the basic tenets of programming are inexistent.

F17 pro price in pakistan black colour

Basically, my program will make a dictionary containing names of properties and their values determined by the main body of the script below. Now, once this dictionary is complete, I want the program to ask the user to give another input. This input is meant to be a key from the dictionary. It will then print the corresponding value out of the dictionary, and then ask the same question again. It's just meant to keep asking the same question,regardless of what it prints or what input is given.

If the input is not a key in the dictionary, it is also meant to repeat the question. Here is my very bad little script in progress. It's undergoing the transformation from printing to dictionary entries, so instead of printing strings that tell you whether it is prime, even etc, it will add them as values to some dictionary key. I hope the inclusion of my incomplete source code helps you to understand my inane ramblings. Sorry for long reply. It works.

Now for the repeating of the question For a program to have repetitive request input, you need to understand while loops, if statements and decision structures. I'll take a look at your code in Shell, but I think I understand it.

If the input is a word, it stops the loop, if it is not a word, it tells you so. I don't understand the first line, though.I keep going with the Coursera course on Python by dr.

Charles Severance. Class is really well constructed and I would warmly recommend it to all programming newbies. It is important to pay attention to details.

I knew that but obviously did not remember lesson well as I was furiously looking for the solution while it was really obvious and would have appear to me much earlier if I would have been more precise. The second experience was a joy of making my longest program so far.

For an experienced programmer this piece of code will look very easy but for me it was the top of the mountain. If you have write the right solution, the right code either for given exercise or some problem of your own, you will know what I am talking about. Enter the numbers from the book for problem 5.

Write a program that prompts the user to enter an integer number n between 1 and 10. (?

I apologize for writing code without proper identation. If you know how could I write nicer lines of code here in wordpress please let me know.

Python Refactoring: "while True" Infinite Loops & The "input" Function

Like Like. In your scenario — you are taking input and testing it at the same time. Like Liked by 1 person. Your opinion was not only informative but also educational for me. I appreciate you sharing this piece of code with me and the readers. You are commenting using your WordPress. You are commenting using your Google account. You are commenting using your Twitter account.

You are commenting using your Facebook account.


thoughts on “Write a program that repeatedly prompts a user for integer numbers

Leave A Comment