Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I need help
#1
Is it possible to get a user to type in something like Apples and it comes back with a number eg

I type apple it comes back as 6?

If so could someone help me do it thank you!
Reply
#2
What have you tried?
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#3
(Aug-15-2017, 02:50 PM)sparkz_alot Wrote: What have you tried?

I'm new to Python and had no lessons so I have tried nothing sorry :(
Reply
#4
HI,

Yes, it is fairly straighforward.

You could use the len function to calculate the number of characters in the word that has been inputted e.g. apple.

x = len(inputted_word)
Where x would be 5 in this case.

Or you could use an if statment to output a unique code or value if the entered word was apple.

if inputted_word == "apple":
    print("!""$%")
or you could replace certain letters in the word apple with other letters.

Let us know which of these you would like help with, or maybe you want other thoughts.

Bass

"The good thing about standards is that you have so many to choose from" Andy S. Tanenbaum
Reply
#5
Bass I want to do the 2nd one :P
Reply
#6
(Aug-15-2017, 02:39 PM)777CAIN Wrote: Is it possible to get a user to type in something like Apples and it comes back with a number eg

I type apple it comes back as 6?

If so could someone help me do it thank you!

Sure!
whatever_they_typed = input("Say something! ")
print(6)
Reply
#7
(Aug-15-2017, 03:40 PM)nilamo Wrote:
(Aug-15-2017, 02:39 PM)777CAIN Wrote: Is it possible to get a user to type in something like Apples and it comes back with a number eg

I type apple it comes back as 6?

If so could someone help me do it thank you!

Sure!
whatever_they_typed = input("Say something! ")
print(6)

Every time they type something different how does it respond different like if i put apple and it says 6 and i put apple123 can it put 12 ect?
Reply
#8
(Aug-15-2017, 03:44 PM)777CAIN Wrote: Every time they type something different how does it respond different like if i put apple and it says 6 and i put apple123 can it put 12 ect?

You could create a dictionary:
my_dict = {'apple':6, 'apple123':12, 'apple_snapple':500}
Of course, you would need to know every possible input a user might enter.  You really need to make your question more specific as to what you are trying to do.
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#9
(Aug-15-2017, 03:53 PM)sparkz_alot Wrote:
(Aug-15-2017, 03:44 PM)777CAIN Wrote: Every time they type something different how does it respond different like if i put apple and it says 6 and i put apple123 can it put 12 ect?

You could create a dictionary:
my_dict = {'apple':6, 'apple123':12, 'apple_snapple':500}
Of course, you would need to know every possible input a user might enter.  You really need to make your question more specific as to what you are trying to do.

I'm trying to do a aircraft CI database where people can put in a airline eg Ryanair and it will say there CI so 6. and every CI if different to every airplane so I want it to do that if possible.
Reply
#10
How do you know that a Ryanair is a 6?

Unless you put it somewhere in your program (or if it's standardized, use a library/service), how do you expect python to know that whatever a "Ryanair" is means whatever meaning you attach to "6"?  To python, they're just unrelated strings/numbers.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020