I have some string from speech recognizer and the value is random but in the same form (x,y...n)
MyOrder = 'orange 2 grape 3 apple 4 mango 5 banana 1'
How to add string and integer into separate list in the correct order like MyOrder? or to filter x,y (string,int or vice versa) along those strings in one line for e.g:
fruit_cart = ['orange', 'grape', 'apple', 'mango', 'banana']
fruit_quantity = [2,3,4,5,1]
I tried to add the list string using conditional if statement, but the order were not the same as MyOrder string.
import numpy as np
import re
import speech_recognition as sr
fruit_cart = []
fruit_quantity = []
fruit_price = []
gross_price = []
recognizer = sr.Recognizer()
with sr.Microphone() as source2:
recognizer.adjust_for_ambient_noise(source2, duration=0.9)
audio2 = recognizer.listen(source2)
MyOrder = recognizer.recognize_google(audio2)
MyOrder = MyOrder.lower()
print("You said: " +MyOrder)
if "apple" in(MyOrder) :
fruit_cart.append("apple")
fruit_price.append(5)
if "banana" in(MyOrder) :
fruit_cart.append("banana")
fruit_price.append(7)
if "grape" in (MyOrder):
fruit_cart.append("grape")
fruit_price.append(9)
if "mango" in (MyOrder):
fruit_cart.append("mango")
fruit_price.append(6)
if "orange" in (MyOrder):
fruit_cart.append("orange")
fruit_price.append(8)
q = [int(x) for x in re.findall('(\d+)', MyOrder)]
fruit_quantity.extend(q)
total_fruit = len(fruit_cart)
print(fruit_cart)
print(fruit_quantity)
print(fruit_price)
print(total_fruit)
gross_price = np.multiply(fruit_quantity,fruit_price)
total_price = 0
for b in range(total_fruit):
print("%i \t %s\t %s \t\t%i\t\t %i" % (b + 1, fruit_cart[b], fruit_price[b], fruit_quantity[b], gross_price[b]))
total_price = total_price + gross_price[b]
tax = total_price * 10/100
final_price = total_price + tax
I'm new to python by the way