-5
user_input = int(input("Please enter a multiplier!"))
if user_input == int:
    for multiplier in range (1,12,1): #this is where i'm kinda stuck
        print multiplier, " x ", user_input, " = ", multiplier * user_input
elif user_input == str:
        print "Please enter a quantitative numerical value as a multiplier, and try again, thanks!"

this is what I am doing: user_input = float(input("Please enter a multiplier!")) for multiplier in range (1,13,1): print multiplier, "x", user_input, " = ", multiplier * user_input

this is the output 1 x 9.9 = 9.9 2 x 9.9 = 19.8 3 x 9.9 = 29.7 4 x 9.9 = 39.6 5 x 9.9 = 49.5 6 x 9.9 = 59.4 7 x 9.9 = 69.3 8 x 9.9 = 79.2 9 x 9.9 = 89.1 10 x 9.9 = 99.0 11 x 9.9 = 108.9 12 x 9.9 = 118.8

#Yet if the user inputs a string (a letter) how should I convert it?
  • Hi. Please at the very least take the time to properly format the code in your question, so we can understand it. Thank you. – Frédéric Hamidi Sep 28 '14 at 16:21
  • If it's not an integer, you'll get a `ValueError`. Have a look at http://stackoverflow.com/q/23294658/3001761 – jonrsharpe Sep 28 '14 at 16:23
  • user_input = int(input("Please enter a multiplier!")) if user_input == int: for multiplier in range (1,12,1): print multiplier, " x ", user_input, " = ", multiplier * user_input elif user_input == str: print "Please enter a quantitative numerical value as a multiplier, and try again, thanks!" – Deborah Israel Sep 28 '14 at 16:25
  • im new here how do you format it? – Deborah Israel Sep 28 '14 at 16:26
  • Don't put code in comments, for a start. http://stackoverflow.com/help/formatting – jonrsharpe Sep 28 '14 at 16:28
  • If you are posting from a PC, there are all kinds of formatting helpers above the text window when you edit the question. – Lev Levitsky Sep 28 '14 at 16:29

1 Answers1

0

You can do this using type or insinstance of python builtin module, like,

if type(user_input) is int:
    # your code

type returns the type of the obect. Or using insinstance,

if isinstance(user_input, int):
    # your code

isinstance return True if the object is instance of a class or it's subclass.

Now, in your code you are already casting the user_input an int in the first line. So there is no point to check if user_input is int or str. The programme will also raise an error if you give an input other than digit.

salmanwahed
  • 9,450
  • 7
  • 32
  • 55
  • please mention the reason for down voting so that the answer can be improved. otherwise there is no point. – salmanwahed Sep 28 '14 at 17:53
  • right, this makes so much sense! but if i want to generate a multiplication timetable depending on the user input and imagine the user inputs a letter then, I wish the program to print an error message saying that a quantitative value is needed. Let me try and portray it, thanks in advance!! – Deborah Israel Sep 28 '14 at 17:57
  • remove the int casting. `user_input = input("Please enter a multiplier!")` – salmanwahed Sep 28 '14 at 18:02
  • excuse my depiction I'm new at this, I did think of doing so but then the output is incorrect, as when multipling 2 by the user input (say 3) will output 33 instead of 6 – Deborah Israel Sep 28 '14 at 18:11
  • So basically I'm defining the input as a numerical value so the program acts as such to generate the multiplication @salmanwahed – Deborah Israel Sep 28 '14 at 18:20
  • Your question was finding the type of your input. Accept the answer if it solves your question. Post new question if you have further problems. – salmanwahed Sep 28 '14 at 18:25
  • Okay how do you think I should ask the question? @salmanwahed please help – Deborah Israel Sep 28 '14 at 18:35
  • Read this: [stackoverflow.com/help/how-to-ask](http://stackoverflow.com/help/how-to-ask). – salmanwahed Sep 28 '14 at 18:38