Learning the ultimate basics of Python, need help with one problem

3    23 Oct 2019 21:25 by u/MrJohnBongo

I am 4 days into learning the very very very ultimate basics of programming, I feel like I'm getting along okay and deciding to write something to see if I could do it, but I'm a bit stuck.

I will try make this quick and simple. I have some values, a=1, b=9, c=3, d=8.

Then I have the following if a=1 print("F"), if b=9 (print("U"), and so on and so on, if the numbers are correct it spells fuck.

However I want to know, if I type in a=2, b=9, c =3, d=8 only a is the incorrect value and the rest will print out as normal. But what I want to do is if just one value is incorrect, then it overrides all of the other print commands and performs a single command saying "wrong passcode" or something.

Bit of a wordy mess but I have no other way of explaining this. Thanks for any help.

15 comments

0

What do you want to do?

  1. Test all the values and then print the text if and only if they all are correct.
  2. Start printing the letters if the first values are right, but stop once a single wrong value is encountered.
0

I made a mistake here, I learned the "and" function, so now it reads

if a=1 and b=4 and c=8 and d=8

print("Correct password. HH")

else:

print("Wrong password")

However, I would like it to wait for me to type each individual correct character, and THEN print in the text. But two problems, I have no idea how to take code I have done in PyCharm into an actual application like in a CMD box for example, nor do I know how to hold off print functions till all values are matched.

0

PyCharm is just running the file you wrote with the python binary, so you should be able to just run python myfile.py from the terminal to get the same output. With regards to reading and then printing, break down the problem into a series of steps:

  1. Read user input into each variable (this step has 4 sub-steps, one for each a-d)
  2. Check each variable (technically has 4 parts, can be done in one line)
  3. Print message depending on 2

I'll give you a hint for 1, you can get input from the keyboard with the function input, e.g. var = input("Enter something").

0

Okay thank you for not spelling it out to me, gonna try figure this one out now, i'll learn more that way!

0

have an upvote for the right attitude

0

No problem man! Honestly, reading API documentation is like 30% of coding, so learning how to figure out where to find it in the first place will help you a lot in the long run haha

0

Finally! I did it hahaha!

a= int(input ("Enter the first digit."))

b= int(input ("Enter the second digit."))

c= int(input ("Enter the third digit."))

d= int(input ("Enter the final digit."))

if a == 1 and b == 4 and c == 8 and d == 8:

print("You have inputted the correct password. HH")

else:

print("Wrong password nigger")

0

You could use something like:

if input("Enter code: ") == "1938":

print("Correct")

0

Use the raw_input() function if using python 2, or input() function if using python 3.

var = input("Prompt text")

Should wait for user input, and return typed string after user presses enter.

Then you can test the string against your passcode and print message

0

You would need a tiered set of if statements:

if a==1:

b=input()

if b==2:

c=input()

if c==3:

~~~etcetc

else:

print("wrong password")

0

if a==1 and b==9 and c==3 and d==8: print("FUCK") else: print("wrong passcode")

0

Shorter:

if (a, b, c, d) == (1, 9, 3, 8)

0

nice. thanks

0

even more compact will try that now, cuts out all of the and functions. thanks

0

a = 1

b = 9

c = 3

d = 8

letters = [a, b, c, d]

for letter in letters:

-if letter = 1:

--print("F")

-if letter = 9:

--print("U")

so on and so forth.