HOW CAN I DO SO, THAT IF I GIVE: "1" IT GIVE ME ONLY "YOU LOVE..." WITHOUT "ENTER A HOSTNAME"?? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

HOW CAN I DO SO, THAT IF I GIVE: "1" IT GIVE ME ONLY "YOU LOVE..." WITHOUT "ENTER A HOSTNAME"??

import random import socket list = ["Nava", "Paula", "Lou"] guest = "" password = "Joker-Tool" print(" Welcome! This is Joker-Tool coded by Joe(CMD)") while guest != password: guest = input("\nEnter Password to use this Tool: ") if guest == password: print("Succes!") else: print("False Password! Please try again!!!") print("\nSelect Option \n[1] Random Love \n[2] IpGetter \n[3] Say-Bot") h = input("\nSelect: ") if h == "1": print("\nYou love " + random.choice(list)) elif h == "2": print(" Welcome! \nThis is a IPGETTER. The Most speedfull app for check the IP of Host's\n***************************CMD******************************") x = input("\nEnter Hostname: ") hostname = x IP = socket.gethostbyname(x) print("Succes!\n" + IP) elif h == "3": print("This is Say-Bot") y = input("\nPrint anyone Greeting (ex.Hi): ") if y == "Hello" or "Hi" or "Ciao": print("Hi. How are u?") else: print("Error")

28th Sep 2018, 1:32 PM
Joe
Joe - avatar
15 Answers
+ 1
you have to indent every line of: x = input("\nEnter Hostname: ") hostname = x IP = socket.gethostbyname(x) print("Succes!\n" + IP)
28th Sep 2018, 1:49 PM
Kevin Dietrichstein
Kevin Dietrichstein - avatar
+ 2
Ok. Verstehst du mein Beispiel oben?
28th Sep 2018, 3:55 PM
Anna
Anna - avatar
+ 2
Das ist eigentlich ziemlich ähnlich. Du hast einen elif-Block und definierst darin eine Variable y: elif h == "3": y = input(...) usw. D.h. die Variable y wird nur definiert, wenn h = 3 ist und der elif-Block ausgeführt wird. Ansonsten wird der elif-Block und alles was darin steht ja einfach übergangen. Wenn h also z.B. 1 ist, wird der o.g. elif-Block nie ausgeführt und Python weiß nicht, was y für eine Variable sein soll. Also kannst du auch nicht ihren Wert prüfen. Sobald du am Zeilenanfang keine Einrückungen mehr machst (Leerzeichen oder Tabs), befindest du dich nicht mehr in dem elif-Block. Wenn die Zeile if y == "Hello" or "Hi" or "Ciao" Bestandteil des elif-Blocks sein soll, musst du das durch die Einrückungen kenntlich machen: elif h == "3": print("This is Say-Bot") y = input("\nPrint anyone Greeting (ex.Hi): ") if y == "Hello" or "Hi" or "Ciao": print("Hi. How are u?") else: print("Error") (achte genau darauf, wie ich die Einrückungen verwende. Alles, was versetzt ist, ist Teil des elif-Blocks. Sobald ich wieder am Zeilenanfang zu schreiben anfange, verlasse ich den elif-Block). Übrigens ist if y == "Hello" or "Hi" or "Ciao" falsch, du musst entweder if y == "Hello" or y == "Hi" or y == "Ciao" schreiben oder if y in ["Hello", "Hi", "Ciao"].
28th Sep 2018, 4:10 PM
Anna
Anna - avatar
+ 1
oh no thanks, i understand!!
28th Sep 2018, 1:51 PM
Joe
Joe - avatar
+ 1
congratz my friend :)
28th Sep 2018, 1:53 PM
Kevin Dietrichstein
Kevin Dietrichstein - avatar
+ 1
The answer to 90% of your questions is: Use correct indentation. See my explanation here, it's a fundamental concept in python: https://www.sololearn.com/Discuss/1515762/?ref=app /Edit: sorry, I had the comments sorted by votes and "send please a code" was the last comment. If your problem is already solved, ignore my comment
28th Sep 2018, 3:31 PM
Anna
Anna - avatar
+ 1
y is declared in the elif block. If the elif block isn't executed, python doesn't know what y is and you can't use it afterwards
28th Sep 2018, 3:46 PM
Anna
Anna - avatar
+ 1
a = 1 if a == 5: b = 'Hello' print(b) This will result in an error. The condition a == 5 is not true. That's why the variable b will never be declared and can't be printed. I'm telling python to print a variable it doesn't know. (Sprichst du deutsch?)
28th Sep 2018, 3:53 PM
Anna
Anna - avatar
0
how cam i do it?
28th Sep 2018, 1:50 PM
Joe
Joe - avatar
0
send please a code
28th Sep 2018, 1:50 PM
Joe
Joe - avatar
0
but if i do so elif h == "3": print("This is Say-Bot") y = input("\nPrint anyone Greeting (ex.Hi): ") if y == "Hello" or "Hi" or "Ciao": print("Hi. How are u?") else: print("Error") it gives an error, if i give "1"
28th Sep 2018, 3:43 PM
Joe
Joe - avatar
0
Im not good in english, example please
28th Sep 2018, 3:50 PM
Joe
Joe - avatar
0
how can i execute it?
28th Sep 2018, 3:54 PM
Joe
Joe - avatar
0
Natürlich! Am Anfang sollten sie fragen
28th Sep 2018, 3:54 PM
Joe
Joe - avatar
0
Nö. Kannst du bitte vlt an meinen code beispiel zeigen, weil dein code ist was anders
28th Sep 2018, 3:56 PM
Joe
Joe - avatar