Sign in to follow this  
mmoWoWmkr

Some Python Questions

Recommended Posts

After alot of advide...Iv'e decided to continue python, but this is not the point. Iv'e been practicing and for the heck of it, I created an extremely easy 'Choose a username and password' program. Here it is below, it does work. ----------------------------------------------- print 'Please provide a username below' a = raw_input('Username: ') print 'Please provide a password below' b = raw_input('Password: ') print 'Your username is', a ,'and your password is', b print 'Please type your password once more below' c = raw_input('Re.Password: ') if c == b: print 'Thank you, the two passwords match' if c != b: print 'Sorry, the two passwords do not match' ------------------------------------------------- My questions for advanced python users are: -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. -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? --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 *? If you can, could you supply an example. Not a whole example. Just something short like 'add the word 'input' in line 5'

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites

cmd = ""
sumlist = []
total = 0
while 1:
cmd = raw_input("Number or stop: ")
if cmd == "stop":
break
else:
sumlist.append(int(cmd))
for item in sumlist:
total += item

print sumlist
print total/len(sumlist)



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

Share this post


Link to 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-number

if len(sumlist) > 0:
print sumlist
print sum(sumlist)/len(sumlist)


Share this post


Link to post
Share on other sites
Ahh. I see where you are going. I tried that in a different way but i got some errors for putting breaks and whiles in the wrong places. I was getting pretty mad, but these two helped alot. Thanks.

Share this post


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

Sign in to follow this