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

Expert Answer

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:

...
What is your favourite number? 4
What is your name? David
Right name, wrong number
help_outline

Image Transcriptionclose

What is your favourite number? 4 What is your name? David Right name, wrong number

fullscreen

Want to see the full answer?

See Solution

Check out a sample Q&A here.

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

Engineering

Computer Science

Python

Related Computer Science Q&A

Find answers to questions asked by student like you

Show more Q&A add
question_answer

Q: Write a program (or modify the one written for Lab 2) that displays the following menu and prompts t...

A: Create a main method and declare all required variables (two variables for operations (+, -, *, /) a...

question_answer

Q: in python 1. Write a program named filemaker.py that will be used to store the first name and age of...

A: Program code:

question_answer

Q: in the program Python  Write a program that the owner of Chica Chic could use to store data about he...

A: A python program is created to store the inventory details in a text file named as inventory.txt. Th...

question_answer

Q: Draw a decision tree and find the number of key comparisons in the worst and average cases for the t...

A: Decision tree (enhanced bubble sort):

question_answer

Q: A palindromic prime is any prime number that is also a palindrome. For example, 131 is a palindromic...

A: ·         Create a main class and required variable to solve the problem of palindromic prime.·     ...

question_answer

Q: In the discussion of ARQ protocol in Section 3.3.3, a scenario was outlined that resulted in the rec...

A: “Yes”, it is possible to allow the receiver to receive multiple copies of the same frame when the me...

question_answer

Q: Find the truth table that describes the following circuit.

A: The diagram shown below is taken from the given question- 

question_answer

Q: PLEASE EXPLAIN ALL STEPSCreate a truth table of the equation:((((Y AND (NOT Z)) OR ((NOT Y) AND Z)) ...

A: Note: In the given equation, the number of opening and closing brackets is not equal. The following ...

question_answer

Q: 1.Write a program in C++ , to count the number of people in each of the following age groups: 0-16 (...

A: Following is the approach of the program to count the number of persons of different age groups:Decl...