3 Replies - 503 Views - Last Post: 17 August 2019 - 04:08 AM Rate Topic: -----

#1 Oleg Nekrassovski   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 2
  • Joined: 29-July 19

Python Exercise

Posted 15 August 2019 - 01:52 PM

Hi. Thanks for taking the time to visit my thread!
So, I am working on the following exercise, and have trouble getting Python (version 2.7.8) to do some of the required things:
Write a program that requests a password. Make the password "hello". The program should then ask the user for their name: if the name entered is the same as your name, the program should respond with "What a great name!"; if they enter "Madonna" or "Cher", the program should respond "May I have your autograph, please?". For any other input, the program should respond with "(input name), thatís a nice name".

SAMPLE OUTPUT:
Program author: Barney
Password? unicorn
Password? opus
Password? hello
Welcome to the second half of the program!
What is your name? Barney
What a great name!

ALTERNATE OUTPUTS:
What is your name? Cher
May I have your autograph, please?
What is your name? Bill
Bill, thatís a nice name.

And here is the program which I wrote to perform all of the above:
def hello():
     print "Welcome to the second half of the program!"
def Oleg():
     print "What a great name!"
def Madonna():
     print "May I have your autograph, please?"
def Cher():
     print "May I have your autograph, please?"


input ("Password? ")
if hello:
     print "Welcome to the second half of the program!"
print""

name = input("What is your name? ")
if name == Oleg:
     print "What a great name!"
elif name == Madonna:
     print "May I have your autograph, please?"
elif name == Cher:
     print "May I have your autograph, please?"
else:                                        
     print (name + ", that's a nice name.")   #This doesn't work!


As you can see, I canít figure out how to make the program which will continue to ask for a password until the correct password is entered.

A more serious problem is that I canít figure out how to make the program, which aside from responding to specific names in specific ways, will also accept any name as input and respond with "(input name), thatís a nice name".

Any help in solving these problems will be much appreciated. Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: Python Exercise

#2 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 11629
  • View blog
  • Posts: 19,760
  • Joined: 19-March 11

Re: Python Exercise

Posted 15 August 2019 - 02:07 PM

There are some pretty fundamental errors here - so basic it's almost hard to know where to start. I suggest you return to the start of your intro lessons and read through them again, carefully, and execute all of the examples you find there.
When I say execute, I mean literally type them in from the page into your editor and run them. This will force you to pay attention to the details, which clearly didn't happen the first time.
Was This Post Helpful? 0
  • +
  • -

#3 Martyr2   User is offline

  • Programming Theoretician
  • member icon

Reputation: 5457
  • View blog
  • Posts: 14,406
  • Joined: 18-April 07

Re: Python Exercise

Posted 16 August 2019 - 01:04 PM

Do remember that input() returns a value and that value is a string. So when you go to compare it against values, you will want to compare against a string right? Is name == Oleg comparing strings? :)

Spoiler

This post has been edited by Martyr2: 16 August 2019 - 01:07 PM

Was This Post Helpful? 1
  • +
  • -

#4 DK3250   User is offline

  • Pythonian
  • member icon

Reputation: 553
  • View blog
  • Posts: 1,730
  • Joined: 27-December 13

Re: Python Exercise

Posted 17 August 2019 - 04:08 AM

Why would you learn python 2.7 ?
It is really old now and many programs do no longer support this version.
I suggest you turn to version 3.7 - it will ensure you are updated.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1