Elifs conditions are not working in my program - python-3.x

This is my code that takes a number of codons. Codons are a group of three nucleotides, each coding for an Amino Acid
codon_sequence=[]
print("Enter no. of codons you want")
n=int(input())
for i in range(n):
codon=str(input())
codon_sequence.append(codon)
print(codon_sequence)
for i in range(n):
if(codon_sequence[i]=="UUU" or "UUC" or "TTT" or "TTC"):
print("Phe_")
elif(codon_sequence[i]=="UUA" or "UUG" or "CUU" or "CUC" or "CUG" or "CUA" or "TTA" or "TTG" or "CTT" or "CTC" or "CTG" or "CTA"):
print("Leu_")
elif(codon_sequence[i]=="UCU" or "UCC" or "UCG" or "UCA" or "AGU" or "AGC" or "TCT" or "TCC" or "TCG" or "TCA" or "AGT" or "AGC"):
print("Ser_")
elif(codon_sequence[i]=="UAU" or "UAC" or "TAT" or "TAC"):
print("Tyr_")
elif(codon_sequence[i]=="UGU" or "UGC" or "TGT" or "TGC"):
print("Cys_")
elif(codon_sequence[i]=="UGG" or "TGG"):
print("Trp_")
elif(codon_sequence[i]=="CCU" or "CCC" or "CCA" or "CCG" or "CCT"):
print("Pro_")
elif(codon_sequence[i]=="CGU" or "CGC" or "CGA" or "CGG" or "AGA" or "AGG" or "CGT"):
print("Arg_")
elif(codon_sequence[i]=="CAU" or "CAC" or "CAT"):
print("His_")
elif(codon_sequence[i]=="CAA" or "CAG"):
print("Gln_")
elif(codon_sequence[i]=="AUU" or "AUC" or "AUA" or "ATT" or "ATC" or "ATA"):
print("Ile_")
elif(codon_sequence[i]=="AUG"):
print("Met_")
elif(codon_sequence[i]=="ACU" or "ACC" or "ACA" or "ACG" or "ACT"):
print("Thr_")
elif(codon_sequence[i]=="GUU" or "GUC" or "GUA" or "GUG" or "GTT" or "GTC" or "GTA" or "GTG"):
print("Val_")
elif(codon_sequence[i]=="GCU" or "GCC" or "GCA" or "GCG" or "GCT"):
print("Ala_")
elif(codon_sequence[i]=="GGU" or "GGC" or "GGA" or "GGG" or "GGT"):
print("Gly_")
elif(codon_sequence[i]=="GAU" or "GAC" or "GAT"):
print("Asp_")
elif(codon_sequence[i]=="GAA" or "GAG"):
print("Glu_")
elif(codon_sequence[i]=="AAU" or "AAC" or "AAT"):
print("Asn_")
elif(codon_sequence[i]=="AAA" or "AAG"):
print("Lys_")
else:
print("Stop_")
This is however, giving me only 'Phe_' as result, and ignores all other conditions

Reason why your code is not hitting the elif blocks
Your if and elif blocks should look like this.
It should check if codon_sequence[i] is equal to a string of interest.
if(codon_sequence[i]=="UUU" or codon_sequence[i]=="UUC" or codon_sequence[i]=="TTT" or codon_sequence[i]=="TTC"):
Instead you have an or condition against just plain strings like UUC.
This will result in the first if condition always being True.
Thereby you will never hit the elif block.
Also a better way of writing the if statement would be:
if codon_sequence[i] in ["UUU", "UUC", "TTT", "TTC"]:
print("Phe_")

This would be a great candidate for a switch statement, but as the previous answer mentioned you can't put an "or" between each string like you're doing.

Related

Generate interleavings of two strings in lexicographical order in Python

How to generate interleavings of two strings in lexicograhical order in python?
I was able to generate interleavings, but not in lexicographical order.
The input given was,
Input :
2
nkb gl
bn zh
Expected output:
Case #1:
glnkb
gnkbl
gnklb
gnlkb
ngkbl
ngklb
nglkb
nkbgl
nkgbl
nkglb
Case #2:
bnzh
bzhn
bznh
zbhn
zbnh
zhbn
This is my code --
def interleave(A,B,ans,m,n,idx):
if m == 0 and n == 0:
print("".join(ans))
return
if len(A)<=len(B):
if m!=0:
ans[idx]=A[0]
interleave(A[1:],B,ans,m-1,n,idx+1)
if n!=0:
ans[idx]=B[0]
interleave(A,B[1:],ans,m,n-1,idx+1)
else:
if n!=0:
ans[idx]=B[0]
interleave(A,B[1:],ans,m,n-1,idx+1)
if m!=0:
ans[idx]=A[0]
interleave(A[1:],B,ans,m-1,n,idx+1)
t=int(input())
count=0
for i in range(t):
count+=1
print("Case #%d:"%count)
A,B=input().split()
m,n=len(A),len(B)
ans=['']*(m+n)
idx=0
interleave(A,B,ans,m,n,idx)
Output of the code that I wrote was--
Case #1:
glnkb
gnlkb
gnkbl
gnklb
nkbgl
nkgbl
nkglb
nglkb
ngkbl
ngklb
Case #2:
bnzh
bznh
bzhn
zhbn
zbnh
zbhn
There is some problem in the logic. Please help me to figure it out.

Output repeating four times

I'm new to python. I'm trying to make a project that orders and sets up combo meals. The problem is that when printing our the receipt, the receipt prints out four times. May I ask for help on how to fix this? I have tried rearranging the code, and trying multiple if statements but none have seem to work.
cart=[]
mains_total=[]
sides_total=[]
drinks_total=[]
sides=[]
drinks=[]
everything_total=[]
stuff={"steak": int(900), "salmon": int(850), "chicken": int(300)}
stuff2={"baked potato": int(80), "mashed potato": int(75), "steamed vegetables": int(50)}
stuff3={"ice tea": int(55), "root beer": int(60), "water": int(20)}
def system():
print("Thanks for using our service, please select an option.\n(1) mains\n(2) sides\n(3) drinks\n(4)
check-out\n(5) exit")
print("Please order one of each category.")
customer_input=int(input("Please type your answer here:"))
if customer_input==1:
print("steak=900")
print("salmon=850")
print("chicken=300")
global mains_order
mains_order=input("What would you like to order?:")
cart.append(mains_order)
system()
if customer_input==2:
print("baked potato=80")
print("mashed potato=75")
print("steamed vegetables=50")
global sides_order
sides_order=input("What would you like to order?:")
sides.append(sides_order)
system()
if customer_input==3:
print("ice tea=55")
print("root beer=60")
print("water=20")
global drinks_order
drinks_order=input("What would you like to order?:")
drinks.append(drinks_order)
system()
if customer_input==4:
for items in cart:
mains_total.append(stuff[items])
global total_sum
total_sum=sum(mains_total)
for items in sides:
sides_total.append(stuff2[items])
global total_sides
total_sides=sum(sides_total)
for items in drinks:
drinks_total.append(stuff3[items])
global total_drinks
total_drinks=sum(drinks_total)
print(cart, total_sum)
print(sides, total_sides)
print(drinks, total_drinks)
everything_total.append(total_sum)
everything_total.append(total_sides)
everything_total.append(total_drinks)
global total_everything
global total_everything
total_everything=(sum(everything_total))
print(total_everything)
global price_input
price_input=int(input("How much would you like to pay?:"))
if '300' '75' '55' in everything_total:
discounted_price=total_everything/100*10
print("this is your discounted price:", discounted_price)
if price_input<total_everything:
print("Sorry you have insuficient funds. Please restate your order and try agian.")
else:
print(cart, total_sum)
print(sides, total_sides)
print(drinks, total_drinks)
print("price total", total_everything)
print("price paid", price_input)
system()

kdb/q: How to apply a string manipulation function to a vector of strings to output a vector of strings?

Thanks in advance for the help. I am new to kdb/q, coming from a Python and C++ background.
Just a simple syntax question: I have a string with fields and their corresponding values
pp_str: "field_1:abc field_2:xyz field_3:kdb"
I wrote an atomic (scalar) function to extract the value of a given field.
get_field_value: {[field; pp_str] pp_fields: " " vs pp_str; pid_field: pp_fields[where like[pp_fields; field,":*"]]; start_i: (pid_field[0] ss ":")[0] + 1; end_i: count pid_field[0]; indices: start_i + til (end_i - start_i); pid_field[0][indices]}
show get_field_value["field_1"; pp_str]
"abc"
show get_field_value["field_3"; pp_str]
"kdb"
Now how do I generalize this so that if I input a vector of fields, I get a vector of values? I want to input ("field_1"; "field_2"; "field_3") and output ("abc"; "xyz"; "kdb"). I tried multiple approaches (below) but I just don't understand kdb/q's syntax well enough to vectorize my function:
/ Attempt 1 - Fail
get_field_value[enlist ("field_1"; "field_2"); pp_str]
/ Attempt 2 - Fail
get_field_value[; pp_str] /. enlist ("field_1"; "field_3")
/ Attempt 3 - Fail
fields: ("field_1"; "field_2")
get_field_value[fields; pp_str]
To run your function for each you could project the pp_str variable and use each for the others
q)get_field_value[;pp_str]each("field_1";"field_3")
"abc"
"kdb"
Kdb actually has built-in functionality to handle this: https://code.kx.com/q/ref/file-text/#key-value-pairs
q){#[;x](!/)"S: "0:y}[`field_1;pp_str]
"abc"
q)
q){#[;x](!/)"S: "0:y}[`field_1`field_3;pp_str]
"abc"
"kdb"
I think this might be the syntax you're looking for.
q)get_field_value[; pp_str]each("field_1";"field_2")
"abc"
"xyz"

Unable to print Dependent vowels

I am reading the text file consisting of bengali words. But I am unable to print the dependent vowels like KA,KI etc...
Here is my sample code and output
import unicodedata
bengali_phoneme_maplist={u'অ':'A',u'আ':'AA',u'ই':'I',u'ঈ':'II',u'উ':'U',u'ঊ ':'UU',u'ঋ ':'R',u'ঌ ':'L',u'এ ':'E',u'ঐ ':'AI',u'ও ':'O',u'ঔ ':'AU',u'ক':'KA',u'খ ':'KHA',u'গ ':'GA',u'ঘ':'GHA',u'ঙ ':'NGA',u'চ ':'CA',u'ছ':'CHA',u'জ ':'JA',u'ঝ':'JHA',u'ঞ':'NYA',u'ট ':'TTA',u'ঠ':'TTHA',u'ড ':'DDA',u'ঢ':'DDHA',u'ণ ':'NNA',u'ত ':'TA',u'ত ':'THA',u'দ':'DA',u'ধ':'DHA',u'ন':'NA',u'প':'PA',u'ফ':'PHA',u'ব':'BA',u'ভ':'BHA',u'ম ':'MA',u'য ':'YA',u'র':'RA',u'ল ':'LA',u'শ ':'SHA',u'ষ':'SSA',u'স ':'SA',u'হ':'ha',u' া ':'AAV',u' ি':'IV',u'ী':'IIV',u'ু':'UV',u'ূ':'UUV',u'ৃ':'RRV',u'ৄ ':'RR',u'ৄ':'EV',u' ৈ':'EV',u'়':'NUKTHA',u'ঽ':'AVAGRAHA'}
bengali_phoneme_maplist_normalise={unicodedata.normalize('NFKD',k):v
for k,v in bengali_phoneme_maplist.items()}
with open('bengali.txt','r')as infile:
lines=infile.readlines()
for index,line in enumerate(lines):
print('Phonemes in line{0}.total{1} symbols'.format(index,len(line)))
unknown=[]
words=line.split()
for word in words:
print(word,':',sep=' ', end='')
for character in word:
c=unicodedata.normalize('NFKD',character).casefold()
try:
print(bengali_phoneme_maplist_normalise[c],sep='',end='')
except KeyError:
print('_',sep='',end='')
if c not in unknown:
unknown.append(c)
print()
if unknown:
print('Unrecognised symbols:{0},total {1} symbols'.format(','.join(unknown),len(unknown)))
Sample input:
শিল্পাঞ্চলে ঢোকার মুখে, স্ন্যাক্সবারে খাবার কিনছিলেন, বহুজাতিক তথ্যপ্রযুক্তি সংস্থার কর্মী, শুভময় বন্দ্যোপাধ্যায়
Sample output:
Phonemes in line0.total129 symbols
text_000002 :___________
"শিল্পাঞ্চলে :_____PA_NYA____
ঢোকার :DDHA_KA_RA
মুখে, :_UV___
স্ন্যাক্সবারে :__NA___KA__BA_RA_
খাবার :__BA_RA
কিনছিলেন, :KA_NACHA___NA_
Unrecognisedsymbols:t,e,x,_,0,2,",শ,ি,ল,্,া,চ,ে,ো,ম,খ,,,স,য,জ,ত,থ,ং,য়,),
(Note that I know nohting about Bengali. :)
There are a few problems in your code:
There are many extra SPACE chars in the bengali_phoneme_maplist definition. For example, u'ঊ ' should be u'ঊ'. And it seems like it's not easy to input chars like u'া' in an text editor so I suggest you directly use unicode in the code, like '\u09be':'AAV'. (Actually I'd suggest you use '\uxxxx' for all chars and write the real chars in comments.)
u'ত':'TA',u'ত':'THA' should change to u'ত':'TA',u'থ':'THA'.
The chars in bengali_phoneme_maplist are not complete. For example there's no ো , ৌ , ্ and ং
After fixing these errors you will get the correct result.

Pull random results from a database?

I have been coding in Python for a 2 months or so, but I mostly ask for help from a more experienced friend when I run in to these kinds of issues. I should also, before I begin, specify that I use Python solely for a personal project; any questions I ask will relate to each other through that.
With those two things out of the way, I have a database of weaponry items that I created using the following script, made in Python 3.X:
#Start by making a list of every material, weapontype, and upgrade.
Materials=("Unobtanium","IvorySilk","BoneLeather","CottonWood","Tin","Copper","Bronze","Gold","Cobalt","Tungsten")
WeaponTypes=("Knife","Sword","Greatsword","Polearm","Battlestaff","Claw","Cane","Wand","Talis","Slicer","Rod","Bow","Crossbow","Handbow","Pistol","Mechgun","Rifle","Shotgun")
Upgrades=("0","1","2","3","4","5","6","7","8","9","10")
ForgeWInputs=[]
#Go through every material...
for m in Materials:
#And in each material, go through every weapontype...
for w in WeaponTypes:
#And in every weapontype, go through each upgrade...
for u in Upgrades:
ForgeWInputs.append((m,w,u))
#We now have a list "ForgeWInputs", which contains the 3-element list needed to
#Forge any weapon. For example...
MAT={}
MAT["UnobtaniumPD"]=0
MAT["UnobtaniumMD"]=0
MAT["UnobtaniumAC"]=0
MAT["UnobtaniumPR"]=0
MAT["UnobtaniumMR"]=0
MAT["UnobtaniumWT"]=0
MAT["UnobtaniumBuy"]=0
MAT["UnobtaniumSell"]=0
MAT["IvorySilkPD"]=0
MAT["IvorySilkMD"]=12
MAT["IvorySilkAC"]=3
MAT["IvorySilkPR"]=0
MAT["IvorySilkMR"]=3
MAT["IvorySilkWT"]=6
MAT["IvorySilkBuy"]=10
MAT["IvorySilkSell"]=5
MAT["CottonWoodPD"]=8
MAT["CottonWoodMD"]=8
MAT["CottonWoodAC"]=5
MAT["CottonWoodPR"]=0
MAT["CottonWoodMR"]=3
MAT["CottonWoodWT"]=6
MAT["CottonWoodBuy"]=14
MAT["CottonWoodSell"]=7
MAT["BoneLeatherPD"]=12
MAT["BoneLeatherMD"]=0
MAT["BoneLeatherAC"]=3
MAT["BoneLeatherPR"]=3
MAT["BoneLeatherMR"]=0
MAT["BoneLeatherWT"]=6
MAT["BoneLeatherBuy"]=10
MAT["BoneLeatherSell"]=5
MAT["TinPD"]=18
MAT["TinMD"]=6
MAT["TinAC"]=3
MAT["TinPR"]=5
MAT["TinMR"]=2
MAT["TinWT"]=12
MAT["TinBuy"]=20
MAT["TinSell"]=10
MAT["CopperPD"]=6
MAT["CopperMD"]=18
MAT["CopperAC"]=3
MAT["CopperPR"]=2
MAT["CopperMR"]=5
MAT["CopperWT"]=12
MAT["CopperBuy"]=20
MAT["CopperSell"]=10
MAT["BronzePD"]=10
MAT["BronzeMD"]=10
MAT["BronzeAC"]=5
MAT["BronzePR"]=3
MAT["BronzeMR"]=3
MAT["BronzeWT"]=15
MAT["BronzeBuy"]=30
MAT["BronzeSell"]=15
MAT["GoldPD"]=10
MAT["GoldMD"]=30
MAT["GoldAC"]=0
MAT["GoldPR"]=5
MAT["GoldMR"]=10
MAT["GoldWT"]=25
MAT["GoldBuy"]=50
MAT["GoldSell"]=25
MAT["CobaltPD"]=30
MAT["CobaltMD"]=10
MAT["CobaltAC"]=0
MAT["CobaltPR"]=10
MAT["CobaltMR"]=0
MAT["CobaltWT"]=25
MAT["CobaltBuy"]=50
MAT["CobaltSell"]=25
MAT["TungstenPD"]=20
MAT["TungstenMD"]=20
MAT["TungstenAC"]=0
MAT["TungstenPR"]=7
MAT["TungstenMR"]=7
MAT["TungstenWT"]=20
MAT["TungstenBuy"]=70
MAT["TungstenSell"]=35
WEP={}
WEP["KnifePD"]=0.5
WEP["KnifeMD"]=0.5
WEP["KnifeAC"]=1.25
WEP["SwordPD"]=1.0
WEP["SwordMD"]=1.0
WEP["SwordAC"]=1.0
WEP["GreatswordPD"]=1.67
WEP["GreatswordMD"]=0.67
WEP["GreatswordAC"]=0.5
WEP["PolearmPD"]=1.15
WEP["PolearmMD"]=1.15
WEP["PolearmAC"]=1.15
WEP["CanePD"]=1.15
WEP["CaneMD"]=1.15
WEP["CaneAC"]=0.7
WEP["ClawPD"]=1.1
WEP["ClawMD"]=1.1
WEP["ClawAC"]=0.8
WEP["BattlestaffPD"]=1.15
WEP["BattlestaffMD"]=1
WEP["BattlestaffAC"]=1.25
WEP["TalisPD"]=1.15
WEP["TalisMD"]=0.7
WEP["TalisAC"]=1.15
WEP["WandPD"]=0.0
WEP["WandMD"]=1
WEP["WandAC"]=1.33
WEP["RodPD"]=0.0
WEP["RodMD"]=1.67
WEP["RodAC"]=0.67
WEP["SlicerPD"]=0.67
WEP["SlicerMD"]=0.67
WEP["SlicerAC"]=0.67
WEP["BowPD"]=1.15
WEP["BowMD"]=1.15
WEP["BowAC"]=0.85
WEP["CrossbowPD"]=1.4
WEP["CrossbowMD"]=1.4
WEP["CrossbowAC"]=1
WEP["PistolPD"]=0.65
WEP["PistolMD"]=0.65
WEP["PistolAC"]=1.15
WEP["MechgunPD"]=0.2
WEP["MechgunMD"]=0.2
WEP["MechgunAC"]=1.5
WEP["ShotgunPD"]=1.3
WEP["ShotgunMD"]=1.3
WEP["ShotgunAC"]=0.4
WEP["RiflePD"]=0.75
WEP["RifleMD"]=0.75
WEP["RifleAC"]=1.75
WEP["HandbowPD"]=0.8
WEP["HandbowMD"]=0.8
WEP["HandbowAC"]=1.2
UP={}
UP["0PD"]=1.0
UP["1PD"]=1.1
UP["2PD"]=1.2
UP["3PD"]=1.3
UP["4PD"]=1.4
UP["5PD"]=1.5
UP["6PD"]=1.6
UP["7PD"]=1.7
UP["8PD"]=1.8
UP["9PD"]=1.9
UP["10PD"]=2.0
UP["0MD"]=1.0
UP["1MD"]=1.1
UP["2MD"]=1.2
UP["3MD"]=1.3
UP["4MD"]=1.4
UP["5MD"]=1.5
UP["6MD"]=1.6
UP["7MD"]=1.7
UP["8MD"]=1.8
UP["9MD"]=1.9
UP["10MD"]=2.0
UP["0AC"]=1.0
UP["1AC"]=1.1
UP["2AC"]=1.2
UP["3AC"]=1.3
UP["4AC"]=1.4
UP["5AC"]=1.5
UP["6AC"]=1.6
UP["7AC"]=1.7
UP["8AC"]=1.8
UP["9AC"]=1.9
UP["10AC"]=2.0
UP["0PR"]=1.0
UP["1PR"]=1.1
UP["2PR"]=1.2
UP["3PR"]=1.3
UP["4PR"]=1.4
UP["5PR"]=1.5
UP["6PR"]=1.6
UP["7PR"]=1.7
UP["8PR"]=1.8
UP["9PR"]=1.9
UP["10PR"]=2.0
UP["0MR"]=1.0
UP["1MR"]=1.1
UP["2MR"]=1.2
UP["3MR"]=1.3
UP["4MR"]=1.4
UP["5MR"]=1.5
UP["6MR"]=1.6
UP["7MR"]=1.7
UP["8MR"]=1.8
UP["9MR"]=1.9
UP["10MR"]=2.0
UP["0WT"]=1.0
UP["1WT"]=0.95
UP["2WT"]=0.9
UP["3WT"]=0.85
UP["4WT"]=0.8
UP["5WT"]=0.75
UP["6WT"]=0.7
UP["7WT"]=0.65
UP["8WT"]=0.6
UP["9WT"]=0.55
UP["10WT"]=0.5
def ForgeW(Material,WeaponType,UpgradeLevel):
"""The ForgeW function Forges a Weapon from its base components into a lethal tool."""
#Get the appropriate material stats...
OrePD=MAT[Material+"PD"]
OreMD=MAT[Material+"MD"]
OreAC=MAT[Material+"AC"]
#And weapon type stats...
SmithPD=WEP[WeaponType+"PD"]
SmithMD=WEP[WeaponType+"MD"]
SmithAC=WEP[WeaponType+"AC"]
#And apply the upgrade...
UpgradePD=UP[UpgradeLevel+"PD"]
UpgradeMD=UP[UpgradeLevel+"MD"]
UpgradeAC=UP[UpgradeLevel+"AC"]
#Then, add them all together.
ProductPD=(OrePD*SmithPD)*UpgradePD
ProductMD=(OreMD*SmithMD)*UpgradeMD
ProductAC=(OreAC*SmithAC)*UpgradeAC
return(ProductPD,ProductMD,ProductAC)
#Recall that ForgeW simply needs its three inputs, which we have a list of. So, let's make our
#database of weapon information.
OmniWeapData={}
#Go through every set of inputs we have...
for Inputs in ForgeWInputs:
#And create a key in the dictionary by combining their three names. Then, set that
#key equal to whatever ForgeW returns when those three inputs are put in.
OmniWeapData[Inputs[0]+Inputs[1]+Inputs[2]] = ForgeW(Inputs[0],Inputs[1],Inputs[2])
I would like to refer to the database created by this code and pull out weapons at random, and frankly I have no idea how. As an example of what I would like to do...
Well, hum. The code in question should spit out a certain number of results based on the complete products of the ForgeW function - if I specify, either within the code or through an input, that I would like 3 outputs, it might output a GoldKnife0, a TinPolearm5, and a CobaltGreatsword10. If I were to run the code again, it should dispense new equipment - not the same three every time.
I apologize if this is too much or too little data - it's my first time asking a question here.
"Take this... it may help you on your quest."
There is a library called random with a method called choice().
e.g.
import random
random.choice([1,2,3])
>>> 2
It sounds like you need one item from Materials, one item from WeaponTypes, and one from Upgrades.
Also, rarely is there ever a need for a triple nested FOR statement. This should get you started.

Resources