How can I write orders with loop, dictionary and if statement by python
If I want the output result from dictionary list and for the others input print a sentence
Here is my program could someone find what is the solution for the mistakes
def phone_guide():
guide_number = input("Enter a number:")
numbers = {"Amal": 1111111111, "Mohammed": 2222222222, "Khadijah": 3333333333, "Abdullah": 4444444444}
While guide_number == 1111111111:
print("Amal")
elif guide_number == 2222222222:
print("Mohammed")
elif guide_number == 3333333333:
print("khadijah")
elif guide_number == 4444444444:
print("Abdullah")
elif guide_number != numbers:
print("Sorry, the number is not found")
#This mean the number has to be from the dictionary list
else:
print("This is invalid number")
#This mean if the number less or more than 10 number or different value or string
phone_guide()
4
Answers
Write your code as above. In python you can only use elif after if.
There are multiple issues you need to fix. The indentation is wrong and the loop has issues. You need to iterate over the dictionary and check if value exists.
First of all, its unneccessarily complicated to store
name: number
if you want to check the number. Is it possible to reverse it likenumber: name
?numbers = {1111111111: "Amal", 2222222222: "Mohammed", ...}
Secondary you don’t need all these if/elif/else statements. You can check if an item is in a dictionaries keys or values.
1111111111 in numbers.keys()
or1111111111 in numbers.values()
To match your phone numbers validity you can use a regex like this and check if
typeof(phone_number) == int
.Given all these information you should be able to come to the solution.
There are multiple errors in your code. Go through the basics of python again.