+ 3

What’s wrong with my code?!?!

I’m messing around with if else statements and while loops and I can’t get my program to work properly. Here is my code: https://sololearn.com/compiler-playground/cho3Gw2aTRXI/?ref=app Bugs: 1. when I enter password correctly on the first attempt it will give me the invalid message. 2. Sometimes after access granted, won’t accept correct answer on the first try and gives me the invalid answer message. 3. It works correctly if you answer “no” but gives invalid answer message for “yes” Please help

2nd Oct 2023, 10:31 PM
Josh Rhodes
7 Respostas
+ 3
Some additional hints from me: (1) When taking the input for the password, there is no way to get out of the loop, if the user can not remember the password. We should give an option (like inputting "x") so that the program can be terminated. (2) There is currently no limitation for the number of input tries for the password. This should be limited by using a counter, and then exit the program prematurely when the counter meets a defined limit. (3) The second input (asking for "yes" or "no") is not running in a loop, so an incorrect answer can not be repeated. We should also give an option (like inputting "x") so that the program can be terminated.
3rd Oct 2023, 11:16 AM
sandra
sandra - avatar
+ 1
Im aware of having to enter inputs on different lines for mulitple prompts. I’m trying to test overall functionality by purposefully entering the incorrect password to get the different messages. You cant do this by running the code in sololearns code playground. You have to copy paste the code into a terminal in codesandbox or idle on your computer
3rd Oct 2023, 12:00 AM
Josh Rhodes
0
Actually you code can run perfectly in the playground with some fix. For the first case, if you input “password”, it evaluated to False and exit the while loop immediately. Therefore “Access granted!” will not printed. For case 2 and 3, would you post your input and the error message for each case so we can tell you what is wrong?
3rd Oct 2023, 1:49 AM
Wong Hei Ming
Wong Hei Ming - avatar
0
I checked your code and found a few problems. First, in the (while) loop, you use (continue) to skip the rest of the loop and start a new iteration. However, this causes the loop to skip the statement (input) that asks for the password again. Instead, you can use (break) to exit the loop and continue with the rest of the code. Second, in the (if) statement that checks if the password is correct, you use (==) to compare the input password with the correct password. However, this comparison is case-sensitive, so if the user enters a password with different letters than the correct password, it will be considered incorrect. To fix this problem, you can convert both strings to lowercase using the lowercase() method before comparing. Third, in the (if) statement that checks if the user wants to continue, you use (or) instead of (and). I was able to understand the code so far, I hope you like it
4th Oct 2023, 7:52 AM
Blunderking2023
Blunderking2023 - avatar
0
There are a few problems with this code: sys.exit(0) should not be printed to the screen, it is a Python built-in function used to exit the program, it should only be used in the if and else blocks to exit the program. if reply == ans_1: print("Excellent! What's your preferred language?") answer = input("Preferred language: ") elif reply == ans_2: print("Sorry to hear that. Have a nice day!") print("Exiting program...") sys.exit(0) else: print("Invalid answer. Please answer yes or no.") reply = input() The second input() after sys.exit(0) is redundant and can be removed. if reply == ans_1: print("Excellent! What's your preferred language?") answer = input("Preferred language: ") elif reply == ans_2: print("Sorry to hear that. Have a nice day!") print("Exiting program...") sys.exit(0) else: print("Invalid answer. Please answer yes or no.") The user input of 'yes' and 'no' should be converted to lowercase before comparison. ans_1 = "yes" ans_2 = "no" if reply.lower() == ans_1: print("Sorry to hear that. Have a nice day!") print("Exiting program...") sys.exit(0) elif reply.lower() == ans_2: print("Excellent! What's your preferred language?") answer = input("Preferred language: ") else: print("Invalid answer. Please answer yes or no.") The variable ans_1 and ans_2 are not defined and thus will raise a NameError. They should be defined above the first if statement.
4th Oct 2023, 2:37 PM
Ava Mohammadpour
Ava Mohammadpour - avatar
0
Ava 91, your interpretation is not correct. First of all sys.exit(0) is not printed. It is a function call. Actually using 'break' will do the job. Second, Josh Rhodes is trying to let the user to answer again if they did not enter 'yes' or 'no'. This part of code should be inside a while loop, and reply = input() is required. Finally, ans_1 and ans_2 was defined before being used. There is no NameError in runtime. However, I do agree the answer inputted should be check in lower case since the variable ans_1 and ans_2 are lower case string.
4th Oct 2023, 2:52 PM
Wong Hei Ming
Wong Hei Ming - avatar