Python Forum
Help with loop & user input
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with loop & user input
#1
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

Reply
#2
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.
Reply
#3
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.
Reply
#4
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
Reply
#5
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.
Recommended Tutorials:
Reply
#6
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.
Reply
#7
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)
Reply
#8
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? ]
Reply
#9
What do think you would do?
Make an effort
Reply
#10
(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
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Simulate an answer based on user input [Beginner needs guidance] Bombardini 1 1,287 Nov-12-2022, 03:47 AM
Last Post: deanhystad
  Print user input into triangle djtjhokie 1 2,377 Nov-07-2020, 07:01 PM
Last Post: buran
  Changing Directory based on user input paulmerton4pope 13 8,015 Aug-14-2020, 11:48 AM
Last Post: GOTO10
  how to add the user input from file into list wilson20 8 4,319 May-03-2020, 10:52 PM
Last Post: Larz60+
  Adding string numbers, while loop and exit without input. Jose 11 7,505 Apr-15-2020, 08:34 AM
Last Post: Jose
  Writing a function that changes its answer based on user input SirRavenclaw 2 2,806 Dec-21-2019, 09:46 PM
Last Post: Clunk_Head
  Print the longest str from user input edwdas 5 4,148 Nov-04-2019, 02:02 PM
Last Post: perfringo
  Question about running comparisons through loop from input value Sunioj 2 2,403 Oct-15-2019, 03:15 PM
Last Post: jefsummers
  how to add user input to a dictionary to a graph KINGLEBRON 3 3,031 Jul-31-2019, 09:09 PM
Last Post: SheeppOSU
  New to Python - tiny coding assistance on user input function and assign to variable Mountain_Duck 1 2,502 Mar-23-2019, 06:54 PM
Last Post: Yoriz

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020