-4

I start to learn python a couple of days ago,Im trying to make some kind of login interface with multiple passwords options but can't figure out the right way to make it loop until I enter the right password ,I tried to use "while" but cant seem to figure out the syntax and placement with it in my code,I want it to repeat the first block of code if "else" is the result so I can try and type the password again,please help?

import random
male = random.choice([True, False])
import random
num = random.choice(["1", "0"])
name = "joe"
user_input= input ("insert Password here ")
if ((user_input == "joey") or (user_input == "loki")):
    if male == True: print("hello")
    if male == False: print("wellcome")
    if name == "joe":
        if num == "1":
            print("hi world")
    if name == "joe":
        if num == "0":
            print("nice")
    if name + num == ("joe" + "0"):
        print("thats working")
else:
  print ("Wrong Password,Please try again.")
euch56
  • 1
  • 1
  • 1
    Does this answer your question? [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – quamrana Sep 13 '20 at 10:26

2 Answers2

0

First of all I would like to point out that you only need to import your libraries once. Second if you want your code to repeat you will require a loop, in python there are 2 types of loops - for loop and while loop. For your requirement I would suggest using a while loop as given below. I couldn't really understand what you were trying to accomplish but I have improved your code a bit.

import random
male = random.choice([True, False])
num = random.choice(["1", "0"])
name = "joe"

while True:
    user_input= input("insert Password here ")
    if (user_input == "joey") or (user_input == "loki"):
        if male == True: 
            print("hello")
        elif male == False: 
            print("wellcome")
        else:
            break

        if name == "joe" and num == "1":
            print("hi world")

        elif name == "joe" and num == "0":
            print("nice")

        if name + num == ("joe" + "0"):
            print("thats working")
        else:
            break
    else:
      print ("Wrong Password,Please try again.")

Another method of having it run repeatedly is to use recursive functions, however since you are just starting out with python I would suggest learning loops before you get into recursive functions.

rishi
  • 643
  • 5
  • 21
  • Generally it's not preferred to use `while True` loops with `break` statements as it can be unclear when the loop ends. The first `break` statement is redundant because `male` is either True or False, it can't be anything else. – Alex Mandelias Sep 13 '20 at 11:02
  • @AAAlex123 Yes I'm aware, thank you for pointing it out. I wanted to provide a solution without functions, to make it as simple as possible. I'm sure this can be edited to the desired needs as necessary. – rishi Sep 13 '20 at 11:08
  • To be honest I didn't really try to accoplish anything with the code,just to experience more with python and solve the wall I hit here with my code haha,thanks alot for the answer,it was very helpful :) – euch56 Sep 13 '20 at 12:49
  • @euch56 No problem and yes you just keep learning and building your interest. Python is a fun language with which you can accomplish a lot. – rishi Sep 13 '20 at 12:52
0

You can use the following general syntax to validate input:

input_to_validate = input(message_on_input)
while not is_valid(input_to_validate):
    print(message_on_error)
    input_to_validate = input(message_on_input)

where is_valid() your method for determining if the input is valid or not

Before coding anything please do learn the language properly and do look up proper syntax conventions. The following code is much more readable and conforms more to coding standards:

import random

male = random.choice([True, False])
num = random.choice(["1", "0"])
name = "joe"

def is_valid(password):
    return password in ["joey", "loki"]


user_input = input("insert Password here: ")

# this while-loop runs as long as the user_input is not valid
while not is_valid(user_input):
    print("Wrong Password, please try again.")
    user_input = input("insert Password here: ")

# this part of the code is reached only when user_input is valid
if male:
    print("hello")
else:
    print("welcome")

if name == "joe":
    if num == "1":
        print("hi world")
    else:
        print("nice")
if name + num == "joe0":
     print("that's working")
Alex Mandelias
  • 436
  • 5
  • 10
  • yeah im still getting the hang of things,you're right about the syntax,I need to learn more on how to make my code more readable and standard,thank you for the comment,will do as you said :) – euch56 Sep 13 '20 at 12:53