• Advertisement

• Popular Now

• 14
• 12
• 9
• 10
• 13
• Advertisement
• Advertisement
• Advertisement

Some Python Questions

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

If you intended to correct an error in the post then please contact us.

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

Share on other sites
Advertisement
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

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

Share on other sites
Can you post your code?

Share this 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

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

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

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

Share this 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

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

Share on other sites

• Advertisement