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 u/DukeofAnarchy 23 Oct 2019 21:36
What do you want to do?
0 u/MrJohnBongo [OP] 23 Oct 2019 21:41
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 u/snafu 23 Oct 2019 21:49
PyCharm is just running the file you wrote with the python binary, so you should be able to just run
python myfile.pyfrom the terminal to get the same output. With regards to reading and then printing, break down the problem into a series of steps: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 u/MrJohnBongo [OP] 23 Oct 2019 21:56
Okay thank you for not spelling it out to me, gonna try figure this one out now, i'll learn more that way!
0 u/watts2db 24 Oct 2019 00:48
have an upvote for the right attitude
0 u/snafu 24 Oct 2019 02:42
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 u/MrJohnBongo [OP] 24 Oct 2019 12:52
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 u/DukeofAnarchy 23 Oct 2019 21:55
You could use something like:
if input("Enter code: ") == "1938":
print("Correct")
0 u/auto_turret 23 Oct 2019 22:55
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 u/BigKyle 29 Nov 2019 23:20
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 u/WillisJaxson 23 Oct 2019 21:39
if a==1 and b==9 and c==3 and d==8: print("FUCK") else: print("wrong passcode")
0 u/i_am_triggered 23 Oct 2019 22:32
Shorter:
if (a, b, c, d) == (1, 9, 3, 8)0 u/WillisJaxson 23 Oct 2019 22:36
nice. thanks
0 u/MrJohnBongo [OP] 24 Oct 2019 11:58
even more compact will try that now, cuts out all of the and functions. thanks
0 u/sherlock_holmes 03 Nov 2019 00:56
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.