0

I start to learn Python. Please give a review to my program:

i = input("input weight = ")

def chicken(i):
    price = 2000
    print('price /kg = ',price)
    totalprice = int(i)*price
    print('total price = ',totalprice)
chicken(i)

This program is run well if I input with number, but get an error with alphabet.

What should I do if I give an alphabet then I can do print("wrong character")?

Red
  • 26,798
  • 7
  • 36
  • 58
  • `(integer/float/deciaml)(*,/,+,-)(integer/float/deciaml)` brother brother, `(integer/float/deciaml)(*,/,+,-)(str, list)` no brother – sahasrara62 Dec 09 '20 at 12:35

2 Answers2

0

This is fairly straightforward problem

i = input("input weight = ")

def chicken(i):
    price = 2000
    print('price /kg = ',price)
    totalprice = int(i)*price
    print('total price = ',totalprice)

if i.isnumeric():
    chicken(i)
else:
    print("wrong character")

Output:

input weight = a
wrong character

input weight = 5
price /kg =  2000
total price =  10000
Dharman
  • 30,962
  • 25
  • 85
  • 135
Srivatsav Raghu
  • 399
  • 4
  • 11
0

try this function

The isnumeric() method returns True if all the characters are numeric (0-9), otherwise False.

i = input("input weight = ")

def chicken(i):
      if not i.isnumeric():
        print("input is not a number")
        return 1
      price = 2000
      print('price /kg = ',price)
      totalprice = int(i)*price
      print('total price = ',totalprice)
chicken(i)
vishal
  • 462
  • 1
  • 5
  • 12