skip to Main Content

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


  1. def phone_guide():
        guide_number = input("Enter a number:")
        numbers = {"Amal": 1111111111, "Mohammed": 2222222222, "Khadijah": 3333333333, "Abdullah": 4444444444}
        if 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")
        else:
            print("This is invalid number")
    

    Write your code as above. In python you can only use elif after if.

    Login or Signup to reply.
  2. 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.

    def phone_guide():
    
        print "Enter a number:"
        guide_number = input()
    
        numbers = {"Amal": 1111111111, "Mohammed": 2222222222, "Khadijah": 3333333333, "Abdullah": 4444444444}
    
        for key,value in numbers.items():
            if guide_number == value:
                print(key)
                return
        print("Sorry, the number is not found")
    
    phone_guide()
    
    Login or Signup to reply.
  3. First of all, its unneccessarily complicated to store name: number if you want to check the number. Is it possible to reverse it like number: 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() or 1111111111 in numbers.values()

    To match your phone numbers validity you can use a regex like this and check if typeof(phone_number) == int.

    import re
    invalid = 1234568
    valid = 0123456789
    re.match(r'd+{10}', valid)
    >>> True
    re.match(r'd+{10}', invalid)
    >>> False
    

    Given all these information you should be able to come to the solution.

    Login or Signup to reply.
  4. There are multiple errors in your code. Go through the basics of python again.

    def phone_guide():
    
      guide_number = int(input("Enter a number:"))
    
      numbers = {"Amal": 1111111111, "Mohammed": 2222222222, "Khadijah": 3333333333, "Abdullah": 4444444444}
    
      for key, value in numbers.items():
        if guide_number == value:
          return key
    
      return "This is invalid number"
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search