+ 1

Need help once more on this code

I want to make this code generates only a limited length of password below 30 numbers. Can anyone help me with this? https://code.sololearn.com/cR5LH0EShEFi/?ref=app

16th Sep 2017, 1:43 AM
Blugon
Blugon - avatar
8 odpowiedzi
+ 1
if you really wanted to use a loop, you could do something like this: import random print("Your password is : ") def check_input(): while True: inp = int(input()) if inp > 30: print('The number is out of range') else: return inp s = check_input() for i in range (s): value = random.randint(1, 9) print(value,end="")
16th Sep 2017, 2:26 AM
ChaoticDawg
ChaoticDawg - avatar
+ 1
You don't need that while loop. Just use the users input if it is less than or equal to 30, otherwise use 30. import random print("Your password is : ") s = int(input()) for i in range (s if s <= 30 else 30): value = random.randint(1, 9) print(value,end="")
16th Sep 2017, 2:14 AM
ChaoticDawg
ChaoticDawg - avatar
+ 1
Thank you very much for the help, really appreciate it :)
16th Sep 2017, 2:24 AM
Blugon
Blugon - avatar
+ 1
Is there any problem with this one? I want to make it print like Your password is : 57292628 Something like that... The print('Your password is: ") prints before the print(value) part https://code.sololearn.com/cR5LH0EShEFi/?ref=app
16th Sep 2017, 2:36 AM
Blugon
Blugon - avatar
+ 1
def s(int(input())) This line isn't right and won't work in the definition of a function. It will see int(input()) as if you're trying to use it as a variable identifier. To define a method/function you use a signature similar to: def function_identifier( parameter_identifier, parameter_identifier, ... etc): You can set set default values also, but they must come as the last parameters in the parameter list. An example of this would be the end='\n' for the print function that you are using.
16th Sep 2017, 2:49 AM
ChaoticDawg
ChaoticDawg - avatar
+ 1
Why should we set while true first?
16th Sep 2017, 2:53 AM
Blugon
Blugon - avatar
+ 1
You don't necessarily always want to use True. I'm using it here to basically say keep doing this over and over until the correct input happens. True also insures that it is done at least once. Python doesn't have a do-while loop like other languages do, and I'm using this in its place.
16th Sep 2017, 2:57 AM
ChaoticDawg
ChaoticDawg - avatar
+ 1
#Can set like before the code is executer, it shows Enter the length of the password that you want to generate #After insert the number, it will become Enter the length of password that you want to generate Your password is : (the numbers) How to make this?
16th Sep 2017, 3:17 AM
Blugon
Blugon - avatar