问题描述:

I have read a number into the program using python. "row[2]", is where the "prices" is, i have tried already to cast it into a int but all it does is print the original number, this is the code.

import time,csv

print ("Welcome to the product search")

print ("To search type yes")

print ("Then type the product code that you are searching for")

print()

print("""

CODE / PRODUCT/ PRICE /

1 SKITTLES £2

2 HARIBO TANGFASTICS £1

3 GUMMY WORMS £3

4 MARSHMALLOWS £1

5 CADBURARY CHOCOLATE £5

6 MAOAM PINBALLS £2

""")

search = input("Search? ")

while search == "yes":

check = 0

file = open("productcodes.csv", "r")#Tells the program which file to search

code = input("Input code here: ")

for row in csv.reader(file):

if row[0] == code:

print(row[1])

check = 1

print()

print("Price: £",row[2])

print()

print("Description: ", row[3])

time.sleep(1)

print()

quantity = int(input("How many of these do you want to order? Input here: "))

price = int(row[2])

amount1 = price*quantity #Multiplies price by the quantity

print(amount1)

if check == 0:

print("No")

search = input("Search? ")

#line = file.readline()

file.close()

Where i am printing the amount the amount just prints the code. Everything else works just that bit doesnt, i need to cast the price variable into an integer!

网友答案:

Your question is little vague. There is no information regarding what and where you want to typecast. In general, for typecasting str to int, do:

x = int('1')

where as for str to float do:

x = float('1.0')
相关阅读:
Top