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")?

Ann Zen
  • 25,080
  • 7
  • 31
  • 51
  • `(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
  • 26,923
  • 21
  • 73
  • 125
Srivatsav Raghu
  • 339
  • 2
  • 10
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
  • 436
  • 1
  • 3
  • 11