# Some Python Questions

This topic is 3936 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

##### Share on other sites
Alright, another dev who has seen the light of Python!

Quote:
 Original post by mmoWoWmkr-Is there any way to make it so that people can type letters AND numbers for an input? Because realy programs dont just use numbers or letters.

raw_input() returns a string object, but you can do anything you want with it. You could convert it to an integer with the int() function, for example.

Quote:
 Original post by mmoWoWmkr-Iv'e probably learnt this already, but i forgot and cant seem to find a tutorial. How do i make it so that in the line where the person puts the wrong password in, it keeps asking the 'Re.Password' until it is put in correctly?

You want to use a "while" loop. This is a very common control flow keyword in many languages. Use it like this:

while True:    c = raw_input('Re.Password: ')    if c == b:        print 'Thank you, the two passwords match'        break    if c != b:        print 'Sorry, the two passwords do not match'

Quote:
 Original post by mmoWoWmkr--And a misc. one that you dont have to answer and i doubt is even possible on python. How do i decode the password? Like replace the password with *?

Check out the getpass module.

- Mike

##### Share on other sites
Ok. I see what you mean. The tutorial i read said that raw_input needs to be only letters, but i tested it with numbers and letters and it worked.

On the other hand. I put in the while True: and it SOMEWHAT WORKED.....but it said, 'Sorry, thw two username do not match' about 1000 times... How can i fix that?

And it only repeated the printed part, it didnt repeat the input.

Thats the last of my questions.

##### Share on other sites
Hi. Doctorsixstring, i have found out the problem so i am all set. I do have one more question. Today, i have decided to do some more practice and make an average finder, where people can type in numbers, and python adds them up and divides by the number of numbers.

My question about this is, is there a way to make it so that, the preson can just keep putting in numbers and after each input there is a line that says 'stop', and when the person types 'stop', python stops asking for numbers and does the last step in which adding all the numbers typed in until stop and counting the number of numbers to divide by?

So let me make it clearer[[[Instead of like typing 5 inputs in the code, there is a never ending list in which once the person types stop, python adds the numbers, counts the amount of inputs, and does it's job]]]

This is not a real example but it would read it like this...

number: 50
stop?
number: 20
stop?
number: 20
stop? stop
50 + 20 + 20 = 90
90 divided by 3 is 30
AVERAGE = 30

##### Share on other sites
You'll want to use another while loop. This time, you'll need to check if the user typed in a number or "stop". You can use the isdigit() method to test if the user typed in a number, or you could simply try to convert it using int() and catch the error in a try..except block.

Keep incrementing a variable with the user-entered integers, and calculate the average after the user types stop.

##### Share on other sites
Could you show me a quick example? I think i know what your saying but im not sure? And since im confused i dont know if you answered how python can count how many numbers were typed in?

##### Share on other sites
You can use a counter variable, and add one to it each time you read in a number.

##### Share on other sites
Ok i sort of get what you said. So it would be like a=0, then when something is input into b,c,d, ect. , 1 gets added to a. Then later when all is finished, it goes like b + c + d + e / a. ???

But i just know how it would read it, if you could put a quick example, i would like that.

I still need someone to answer this...How could i make it so that, the person can type as many numbers and possible (infinate amount of inputs) and something like...after each input it says stop, and when the person types stop, it stops and makes the average.

##### Share on other sites
You only need two variables: one to store the total sum of all typed values, and another to indicate the total count of entered numbers.

##### Share on other sites
cmd = ""sumlist = []total = 0while 1:    cmd = raw_input("Number or stop: ")    if cmd == "stop":        break    else:        sumlist.append(int(cmd))for item in sumlist:    total += itemprint sumlistprint total/len(sumlist)

I wrote this about 6 hours ago when I first saw your post.

##### Share on other sites
That code looks reasonable. I have a few thoughts, though...

1) You don't need to declare variables before usage assignment in Python, so the first line in the posted code (cmd = "") isn't necessary.

2) You can use the sum() function to calculate the sum of a sequence (i.e. a list, tuple, set, etc.).

3) The user input should be processed inside a try..except block. Exception handling is very useful.

Since you've let the cat out of the bag and posted some code, I'll post a version with the above changes:

sumlist = []while 1:    try:        cmd = raw_input("Number or stop: ")        sumlist.append(int(cmd))    except:        break  # stop if the user entered a non-numberif len(sumlist) > 0:	print sumlist	print sum(sumlist)/len(sumlist)