Jump to content

User is asked to input numbers until certain value is met

Hafiz75

Is there a way to asked the user to input a number of there choice. Let’s take 4 for example. And that number is the number of lines. And the user has to input numbers or characters they want in each line. Let’s say on line 1 then enter AADD123. Then the program keeps asking the user to input what they want on each line until it reaches the number they have entered. Which for our example is 4 lines of numbers.

The  program I would like to us in Python 3

CPU: Ryzen 5 2600X | Motherboard: MSI B450M Mortar (Black) RAM: Corsair Vengeance LPX 16GB (2x 8GB) 3000MHz DDR4 | GPU: MSI Radeon RX 580 ARMOR 8GB | Storage: Samsung 860 QVO 1TB and Samsung 970 EVO 250GB | PSU: Corsair CX650M 650W Case: Cooler Master MasterBox Q300L | Monitor: ASUS VZ279HE | Mouse: Logitech M171 Wireless | Keyboard: Cherry DC 2000 | Mousepad: None | Speakers: Logitech Z333

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, Slottr said:

Sure you can. How you'll do it depends on the language though.

Python 

CPU: Ryzen 5 2600X | Motherboard: MSI B450M Mortar (Black) RAM: Corsair Vengeance LPX 16GB (2x 8GB) 3000MHz DDR4 | GPU: MSI Radeon RX 580 ARMOR 8GB | Storage: Samsung 860 QVO 1TB and Samsung 970 EVO 250GB | PSU: Corsair CX650M 650W Case: Cooler Master MasterBox Q300L | Monitor: ASUS VZ279HE | Mouse: Logitech M171 Wireless | Keyboard: Cherry DC 2000 | Mousepad: None | Speakers: Logitech Z333

Link to comment
Share on other sites

Link to post
Share on other sites

Sure.

  1. Ask them how many lines they want, store this in a variable
  2. Use a loop with the exit condition based on the variable (e.g., a while-loop with the exit condition being variable > 0 and decrement the variable at the end of the loop)
  3. In the loop, keep asking for whatever you wanted
Link to comment
Share on other sites

Link to post
Share on other sites

Just now, Mira Yurizaki said:

Sure.

  1. Ask them how many lines they want, store this in a variable
  2. Use a loop with the exit condition based on the variable
  3. In the loop, keep asking for whatever you wanted

Thanks for the help but I still can’t wrap my head around the question. I’ve been sitting down for 1 hour and still had zero idea. That’s why I came to Linus Tech Tips Forum for someone for help with the code.

CPU: Ryzen 5 2600X | Motherboard: MSI B450M Mortar (Black) RAM: Corsair Vengeance LPX 16GB (2x 8GB) 3000MHz DDR4 | GPU: MSI Radeon RX 580 ARMOR 8GB | Storage: Samsung 860 QVO 1TB and Samsung 970 EVO 250GB | PSU: Corsair CX650M 650W Case: Cooler Master MasterBox Q300L | Monitor: ASUS VZ279HE | Mouse: Logitech M171 Wireless | Keyboard: Cherry DC 2000 | Mousepad: None | Speakers: Logitech Z333

Link to comment
Share on other sites

Link to post
Share on other sites

3 minutes ago, Mira Yurizaki said:

Sure.

  1. Ask them how many lines they want, store this in a variable
  2. Use a loop with the exit condition based on the variable (e.g., a while-loop with the exit condition being variable > 0 and decrement the variable at the end of the loop)
  3. In the loop, keep asking for whatever you wanted

Could you help me with the code step by step. I’ve done the first part.

 

1.lines= int(input(“How many lines do you want”))

CPU: Ryzen 5 2600X | Motherboard: MSI B450M Mortar (Black) RAM: Corsair Vengeance LPX 16GB (2x 8GB) 3000MHz DDR4 | GPU: MSI Radeon RX 580 ARMOR 8GB | Storage: Samsung 860 QVO 1TB and Samsung 970 EVO 250GB | PSU: Corsair CX650M 650W Case: Cooler Master MasterBox Q300L | Monitor: ASUS VZ279HE | Mouse: Logitech M171 Wireless | Keyboard: Cherry DC 2000 | Mousepad: None | Speakers: Logitech Z333

Link to comment
Share on other sites

Link to post
Share on other sites

13 minutes ago, Mira Yurizaki said:

What do you know about loops?

I literally have no idea how do do this part because my organs will have to store the numbers that are being inputed. Please could you tech me how this is done. 

CPU: Ryzen 5 2600X | Motherboard: MSI B450M Mortar (Black) RAM: Corsair Vengeance LPX 16GB (2x 8GB) 3000MHz DDR4 | GPU: MSI Radeon RX 580 ARMOR 8GB | Storage: Samsung 860 QVO 1TB and Samsung 970 EVO 250GB | PSU: Corsair CX650M 650W Case: Cooler Master MasterBox Q300L | Monitor: ASUS VZ279HE | Mouse: Logitech M171 Wireless | Keyboard: Cherry DC 2000 | Mousepad: None | Speakers: Logitech Z333

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, Safeer Hafiz said:

I literally have no idea how do do this part because my organs will have to store the numbers that are being inputed. Please could you tech me how this is done. 

https://wiki.python.org/moin/ForLoop

https://wiki.python.org/moin/WhileLoop

 

Also since I mentioned "exception": https://docs.python.org/3/tutorial/errors.html

Link to comment
Share on other sites

Link to post
Share on other sites

5 minutes ago, Mira Yurizaki said:

Please could you just summaries all of that for me. Much appreciated 

CPU: Ryzen 5 2600X | Motherboard: MSI B450M Mortar (Black) RAM: Corsair Vengeance LPX 16GB (2x 8GB) 3000MHz DDR4 | GPU: MSI Radeon RX 580 ARMOR 8GB | Storage: Samsung 860 QVO 1TB and Samsung 970 EVO 250GB | PSU: Corsair CX650M 650W Case: Cooler Master MasterBox Q300L | Monitor: ASUS VZ279HE | Mouse: Logitech M171 Wireless | Keyboard: Cherry DC 2000 | Mousepad: None | Speakers: Logitech Z333

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, Safeer Hafiz said:

Please could you just summaries all of that for me. Much appreciated 

You should really read about loops, because they are a fundamental element of programming.

 

As for exceptions, it's just a way for the program to declare an input, even though technically it's what something asked for, cannot produce the desired result. For example int() accepts some data, but if it cannot actually convert the data into an integer, it throws an exception. So if you feed int() "123abcd", it will throw an exception because it cannot convert the letters into numbers. The driving principle for using exceptions in Python is "it's easier to ask for forgiveness than permission."

 

Either way, the code that you have as-is will cause the app to break if you input something other than a number, and you should account for this.

Link to comment
Share on other sites

Link to post
Share on other sites

6 minutes ago, Mira Yurizaki said:

I’ve read through the pages but they dint help me with what I’m looking for. Because I have to store the number that user has imputed in a variable.

CPU: Ryzen 5 2600X | Motherboard: MSI B450M Mortar (Black) RAM: Corsair Vengeance LPX 16GB (2x 8GB) 3000MHz DDR4 | GPU: MSI Radeon RX 580 ARMOR 8GB | Storage: Samsung 860 QVO 1TB and Samsung 970 EVO 250GB | PSU: Corsair CX650M 650W Case: Cooler Master MasterBox Q300L | Monitor: ASUS VZ279HE | Mouse: Logitech M171 Wireless | Keyboard: Cherry DC 2000 | Mousepad: None | Speakers: Logitech Z333

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, Safeer Hafiz said:

I’ve read through the pages but they dint help me with what I’m looking for. Because I have to store the number that user has imputed in a variable.

Which you already did, so now we have to use the variable to repeatedly ask the user to input something. Loops are the best way to do this.

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, Mira Yurizaki said:

Which you already did, so now we have to use the variable to repeatedly ask the user to input something. Loops are the best way to do this.

Yes but when I loop it, it will to write over the variable. I want o to create new variable.

CPU: Ryzen 5 2600X | Motherboard: MSI B450M Mortar (Black) RAM: Corsair Vengeance LPX 16GB (2x 8GB) 3000MHz DDR4 | GPU: MSI Radeon RX 580 ARMOR 8GB | Storage: Samsung 860 QVO 1TB and Samsung 970 EVO 250GB | PSU: Corsair CX650M 650W Case: Cooler Master MasterBox Q300L | Monitor: ASUS VZ279HE | Mouse: Logitech M171 Wireless | Keyboard: Cherry DC 2000 | Mousepad: None | Speakers: Logitech Z333

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, Mira Yurizaki said:

Look at the loop pages again, one of them will let you do what you want to keep track of how many times the loop has executed.

Please could you indicate where about that would be. For loops or while loops page

 

 

CPU: Ryzen 5 2600X | Motherboard: MSI B450M Mortar (Black) RAM: Corsair Vengeance LPX 16GB (2x 8GB) 3000MHz DDR4 | GPU: MSI Radeon RX 580 ARMOR 8GB | Storage: Samsung 860 QVO 1TB and Samsung 970 EVO 250GB | PSU: Corsair CX650M 650W Case: Cooler Master MasterBox Q300L | Monitor: ASUS VZ279HE | Mouse: Logitech M171 Wireless | Keyboard: Cherry DC 2000 | Mousepad: None | Speakers: Logitech Z333

Link to comment
Share on other sites

Link to post
Share on other sites

5 minutes ago, Mira Yurizaki said:

Look at the loop pages again, one of them will let you do what you want to keep track of how many times the loop has executed.

I’ve read both pages but still can’t find what your on about. Please could you just explain to me.

CPU: Ryzen 5 2600X | Motherboard: MSI B450M Mortar (Black) RAM: Corsair Vengeance LPX 16GB (2x 8GB) 3000MHz DDR4 | GPU: MSI Radeon RX 580 ARMOR 8GB | Storage: Samsung 860 QVO 1TB and Samsung 970 EVO 250GB | PSU: Corsair CX650M 650W Case: Cooler Master MasterBox Q300L | Monitor: ASUS VZ279HE | Mouse: Logitech M171 Wireless | Keyboard: Cherry DC 2000 | Mousepad: None | Speakers: Logitech Z333

Link to comment
Share on other sites

Link to post
Share on other sites

8 minutes ago, Mira Yurizaki said:

Look at the loop pages again, one of them will let you do what you want to keep track of how many times the loop has executed.

Oh. I don’t think your going to reply now. ?. I’ll find someone else how can help me.

CPU: Ryzen 5 2600X | Motherboard: MSI B450M Mortar (Black) RAM: Corsair Vengeance LPX 16GB (2x 8GB) 3000MHz DDR4 | GPU: MSI Radeon RX 580 ARMOR 8GB | Storage: Samsung 860 QVO 1TB and Samsung 970 EVO 250GB | PSU: Corsair CX650M 650W Case: Cooler Master MasterBox Q300L | Monitor: ASUS VZ279HE | Mouse: Logitech M171 Wireless | Keyboard: Cherry DC 2000 | Mousepad: None | Speakers: Logitech Z333

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, Mira Yurizaki said:

Here's an example:


foobar = 10

for x in xrange(0, foobar, 1):
  print x

 

???. There no inputs. 

CPU: Ryzen 5 2600X | Motherboard: MSI B450M Mortar (Black) RAM: Corsair Vengeance LPX 16GB (2x 8GB) 3000MHz DDR4 | GPU: MSI Radeon RX 580 ARMOR 8GB | Storage: Samsung 860 QVO 1TB and Samsung 970 EVO 250GB | PSU: Corsair CX650M 650W Case: Cooler Master MasterBox Q300L | Monitor: ASUS VZ279HE | Mouse: Logitech M171 Wireless | Keyboard: Cherry DC 2000 | Mousepad: None | Speakers: Logitech Z333

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, Mira Yurizaki said:

Okay, level with me

 

Do you want me to teach you the basics of programming so you can solve this on your own, or do you just want me to provide the source code that'll do what you want?

Second option would be preferred.

CPU: Ryzen 5 2600X | Motherboard: MSI B450M Mortar (Black) RAM: Corsair Vengeance LPX 16GB (2x 8GB) 3000MHz DDR4 | GPU: MSI Radeon RX 580 ARMOR 8GB | Storage: Samsung 860 QVO 1TB and Samsung 970 EVO 250GB | PSU: Corsair CX650M 650W Case: Cooler Master MasterBox Q300L | Monitor: ASUS VZ279HE | Mouse: Logitech M171 Wireless | Keyboard: Cherry DC 2000 | Mousepad: None | Speakers: Logitech Z333

Link to comment
Share on other sites

Link to post
Share on other sites

5 minutes ago, Safeer Hafiz said:

Second option would be preferred.

 

lines_input = input("Enter number of lines you want: ")

while (str.isdigit(lines_input) == False):
    lines_input = input("Enter number of lines you want: ")

lines = int(lines_input)
user_input = []

for x in range(lines):
    user_input.append(input("Enter some text (Line %d): " % (x + 1)))

for line in user_input:
    print(line)

 

Link to comment
Share on other sites

Link to post
Share on other sites

2 hours ago, Safeer Hafiz said:

Second option would be preferred.

Wow... You literally could've saved the whole entire effort by googling your question and stealing the first thing you find in Stack Overflow.

 

If you don't care to learn, then why are you doing basic homework problems for an entry level course?

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×