# Make this code more efficient numb = int(input("What is your favourite number? "))name = str(input("What is your name? "))if numb == 7 and name != ("David"):    print ("correct number, wrong name! you silly bean!!!!")elif numb !=7 and name == ("David"):    print ("Right name, wrong number")elif numb ==7 and name ==("David"):    print ("got'em both!")elif numb != 7 and name != ("David"):    print ("neither")else:    print("zzzzzz") We have used NOT with the AND statements to get what we want as a response. Is there a more efficient way of coding this program?  Make the above code more efficient

Question
Asked Oct 24, 2019
15 views
Make this code more efficient

numb = int(input("What is your favourite number? "))
name = str(input("What is your name? "))
if numb == 7 and name != ("David"):
print ("correct number, wrong name! you silly bean!!!!")
elif numb !=7 and name == ("David"):
print ("Right name, wrong number")
elif numb ==7 and name ==("David"):
print ("got'em both!")
elif numb != 7 and name != ("David"):
print ("neither")
else:
print("zzzzzz")

We have used NOT with the AND statements to get what we want as a response. Is there a more efficient way of coding this program?

Make the above code more efficient
check_circle

Step 1

To make this code more efficient do not use NOT and AND operation instead of this use nested if to make it more efficient. Element last else part from code because this program will never come in this else part at any of the situation.

Step 2

Program:

numb = int(input("What is your favourite number? "))

name = str(input("What is your name? "))

if numb == 7:

if name==("David"):

print ("got'em both!")

else:

print ("correct number, wrong name! you silly bean!!!!")

elif name==("David"):

if numb == 7:

print ("got'em both!")

else:

print ("Right name, wrong number")

else:

print ("neither")

Step 3

Output:

...

### Want to see the full answer?

See Solution

#### Want to see this answer and more?

Solutions are written by subject experts who are available 24/7. Questions are typically answered within 1 hour.*

See Solution
*Response times may vary by subject and question.
Tagged in