Python Forum

Full Version: Help with loop & user input
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
This is Not homework assignment. I am beginner to programing itself, just started with python.

What i want to achieve is this:
  • Ask a question to user.
  • Get answer to above from user (numbers only)
  • Based on above answer, create something:
    what it that something should be?
    • a list
    • a variable
    • a dictionary
  • When something is created, that something will be getting user input till it reaches the value given at the first input ( i.e. at step which is marked in bold)

Example:
  • A. How many cars, you have given on rent?
  • B. This is user input, which only should accept numbers.
    > Ans/Value Input by user : 5   
    [# So the user has given 5 cars on rent.]
  • C. Now I want the user to input rental income of each of those 5 cars.

    > incomeFromCarOne = 1200   
    > incomeFromCarTwo = 2450 etc...    
    > till incomeFromCarFive is reached. 
  • D. If user input value is nil/zero or blank, the script should stop.
    [# What i mean by that is, script should go to next line of code which is independent of above.]
I could post code but it's basically getting input from user at B and that's it, i can not get past the first input.


System Info:
1. Win 7
2. Python 3.5

We need to see your attempt whether it's homework or not, since we can't give a very helpful answer otherwise without writing the code for you. Please provide a short snippet of code showing your attempt at something, along with a description what what you want it to do and what it actually does. You can check out this too, if you're stuck.
I am trying, basically what output i want is:

Question    : How many cars you have given on rent?
User_Input : 3

then based on input, the script will generate following:
incomeFromCarOne [# prompts for user input]
incomeFromCarTwo [# prompts for user input]
incomeFromCarThree [# prompts for user input]

if user inputs 0 then on to next independent question. if 10, then incomesFor-10 cars. etc.

sorry for no code...but its hard.
Sure it's hard, but show us what you've got so far, and we'll help fix the issues. But we won't write it for you lol
Quote:I could post code but it's basically getting input from user at B and that's it, i can not get past the first input.
i can tell you right now, when someone posts a question and does not post their code, i dont even bother answering. And i can assure you others feel the same way too. 

The only reason i am posting at all is to tell you that i moved your post to homework. Whether or not you really are doing homework is not the point....it is close enough to merit being in that forum. I can tell you though, the way you ask it, portray it, not post code in it, homework is exactly what it looks like.
hey thanks for reply........
I am from finance background, regardless

I am learning coding as hobby.
So you guys are my teachers in someway. So, yes we all can agree as this being homework.
now assuming that this is homework, can any one give me idea as how to proceed??

do i need a list or dictionary or variable & or define a function?

What i had done?

print('How many cars do you have? ')
user_input = input()

if user_input == int:
    pass
else:
    print('Please input number only. ')

def hmCars(howManyCars):
    for x in user_input:
        if user_input <= 0:
            break
        else:
            for x in user_input
how i can loop through user_input ?

ok '0' is not an integer. So i don't need the break statement.
hmCars is never called so can't execute
you need to do something like:
user_input = None

def hmCars(num):
    print(num)

while(not user_input):
    user_input = int(input('How many cars do you have? '))
    hmCars(user_input)
thank you Larz60+
now how i can loop through the input value?

do i need to create a 'list' for that or 'a dictionary'? so that if user inputs 5 as his cars, i can loop through it for getting each of 5 cars rental income as user input?
[# do the dictionary or list has to be before the def of hmCars & while statement access it? ]
What do think you would do?
Make an effort
(Dec-23-2016, 04:20 AM)Larz60+ Wrote: [ -> ]hmCars is never called so can't execute
you need to do something like:
user_input = None

def hmCars(num):
    print(num)

while(not user_input):
    user_input = int(input('How many cars do you have? '))
    hmCars(user_input)

Since the user can input everything he/she wants is more safe to get the input in try statement.

    try:
        user_input = int(input('How many cars do you have? '))
    except:
        break
Pages: 1 2