I was making code on python but it showed an asterisk where the number goes on the cell, I tried making a print program to see if it was the code but it still didn't work. Please help, this is the code.
Items = ""
Total = 0
def adding_report(report):
while True:
X = input("please input integer to add or Q to quit")
if X.isdigit() == "True":
X = int(X)
Total = Total + X
if report == "A":
Items = Items + X
elif X == "Q":
print("Your result is")
if report == "A":
print("Items")
print(Items)
print("total")
print(Total)
break
else:
print("invalid input.")
adding_report("T")
You are stuck in an infinite loop.
Moreover, you cannot compare to the string "True", but rather to True only:
if X.isdigit() == True:
Instead of:
if X.isdigit() == "True":
You can also skip the comparison to True altogether
if X.isdigit():
Related
import random
given_number = round(float(input("Enter a number;\n")))
loop = True
while loop:
def Possibility(maximum):
count = 0
while count == 0:
x = random.randint(0, maximum)
if x:
print("X is True")
else:
print("X is false")
count += 1
print("Possibility calculated")
answer = input("Do you want to try again? Y/N")
if answer == "N":
loop = False
Possibility(given_number)
When I run the code even if I type N to answer as input program still continues to run any idea why this is happening?
import random
given_number = round(float(input("Enter a number;\n")))
def Possibility(maximum):
count = 0
while count == 0:
x = random.randint(0, maximum)
print(x)
if x:
print("X is True")
return True
else:
print("X is false")
count += 1
print("Possibility calculated")
answer = input("Do you want to try again? Y/N")
if answer == "N":
return False
while Possibility(given_number):
given_number = round(float(input("Enter a number;\n")))
Possibility(given_number)
the issue is, is that the function possibility was in a while loop, which means that the function would not be executed until it ended, which it couldnt, as the end condition was in the function.
this should fix your issue, by making it loop the function itself rather than the definition.
to break out of the loop, you would use the return function, which is an easy way to end the loop in this case.
So, I’m stuck with separating the multiple loops.
The first cycle works fine. And the second one too. But not the two last ones. When I’m trying to type the DNA or Protein, it only gives an answer: Restart? Y \ N
My code:
while True:
A = (input())
if A == ("Start"):
print ("What type of the data you want to process?")
print ("DNA", "RNA", "Protein", sep="\n")
break
else:
print ("Incorrect input")
continue
break
while True:
X = (input())
if X == ("RNA"):
print ("What you want to do with RNA?", "RNA to DNA? RNA to Protein? seqRNA length?", sep="\n?")
continue
break
else:
print("Restart?", "Y \ N", sep="\n")
continue
break
while True:
Y = (input())
if Y == ("DNA"):
print ("What you want to do with DNA?", "DNA to RNA? seqDNA length?", sep="\n")
continue
break
else:
print("Anything else?", "Yes No", sep="\n")
continue
break
while True:
Z = (input())
if Z == ("Protein"):
print ("What you want to do with Protein?", "Protein to RNA? protein Name?", sep="\n")
continue
break
else:
print("Anything else?", "Yes No", sep="\n")
continue
break
I’m already tried to delete one of these cycles and tried to run it, but the results were the same. I had searched for the examples of using break, while and if, but found answers only for just the only one loop, not multiple. I find it quite dissapointing, because I’ve wanted to do this for a better understanding of the python. And not just using a BioPython library. But ironically, just the process of the transcription, reverse transcription, translation, translation to RNA, or sequence length calculation is not a big problem. It’s all needs just a one line of the code.
I’ve cleaned up and restructured your code a bit. Is this what you wanted?
while True:
print("What type of the data you want to process?")
A = input("DNA, RNA, Protein > ")
if A == "DNA":
print("What you want to do with DNA?")
Y = input("DNA to RNA? seqDNA length? > ")
# Test and do something with Y
elif A == "RNA":
print("What you want to do with RNA?")
X = input("RNA to DNA? RNA to Protein? seqRNA length? > ")
# Test and do something with X
elif A == "Protein":
print("What you want to do with Protein?")
Z = input("Protein to RNA? protein Name? > ")
# Test and do something with Z
else:
print("Incorrect input")
continue
# If user wants to restart, stay in the loop, else break.
if input("Restart? Y \ N > ") == "N":
break
The problem is with the continue statements once you get passed the Start condition and rna/dna/protein condition.
I only tested for RNA but I removed the continue statement after the print and it seemed to work
what is your intention with the continue there?
Additionally, using so many while true loops and relying on break and continue isn't really best practice idt. I would use do something like use A as input at the very top and your conditions could be something like `while A not in ["RNA", "DNA", PROTEIN"] or something like that and that way it'll keep looping and asking for input until correct one is passed
finally, I would cast every input to be uppercase or lowercase (and your conditional checks too ) that way you can avoid input errors based on case issues
while True:
print("What type of the data you want to process?", "RNA? DNA?", sep = "\n")
A = input("")
if A == "RNA":
print("What you want to do with RNA?", "(a1)RNA to Protein? (a2)RNA to DNA? (a3)RNA Length? (a4)RNA Splicing? (a5)RNA nucleotides (a6)ratio? RNAi? ", sep = "\n")
break
if A == "DNA":
print("What you want to do with DNA", "(b1)DNA to RNA? (b2)DNA length? (b3)DNA nucleotides ratio? (b4)Complimentary DNA chain?", sep = "\n")
break
else:
print("Nope") #TF2 references
continue
while A == "RNA":
X = input("")
if X == "a1":
print("Input RNA")
import ptranslation #Written by myself module for RNA translation
RNA_translation = input("")
print (RNA_translation)
break
if X == "a2":
print("Input RNA")
RNA_seq = input("")
DNA_seq = RNA_seq.replace("U", "T")
print (DNA_seq)
break
if X == "a3":
print ("Input RNA")
RNA = input("")
RNAo = int (len(RNA)//2)
print(RNAo)
break
if X == "a4":
print ("Input RNA")
import re
RNA = input("")
regex = r"GU(?:\w{0,}?)AG" #regex black magic Uga-Buga, still need to undestand it better
exons = re.sub(regex, '', RNA)
print(exons)
break
if X == ("a5"):
print("Input RNA")
RNAn = input ("")
X = (RNAn.count("A")) + (RNAn.count("U")) + (RNAn.count("C")) + (RNAn.count ("G"))
A = (RNAn.count("A")/X)
U = (RNAn.count("U")/X)
G = (RNAn.count("G")/X)
C = (RNAn.count("C")/X)
print ("A =", A, "U =", U, "G =", G, "C =", C)
break
if X == ("a6"):
print("Input RNA")
N = input("")
print(N.translate(str.maketrans({"A": "U", "G": "C", "U": "A", "C": "G"})))
break
else:
print("NOPE! Input correct option!")
continue
break
while A == "DNA":
Y = input("")
if Y == "b1":
print ("Input DNA ")
DNA_seq = input("")
RNA_seq = DNA_seq.replace("T", "U")
print (RNA_seq)
break
if Y == ("b2"):
print("Input DNA")
DNA = input("")
DNAo = int (len(DNA)//2)
print(DNAo)
break
if Y == ("b3"):
print("Input DNA")
DNAn = input ("")
X = (DNAn.count("A")) + (DNAn.count("T")) + (DNAn.count("C")) + (DNAn.count ("G"))
A = (DNAn.count("A")/X)
T = (DNAn.count("T")/X)
G = (DNAn.count("G")/X)
C = (DNAn.count("C")/X)
print ("A =", A, "T =", T, "G =", G, "C =", C)
break
if Y == ("b4"):
print("Input DNA")
Nd = input("")
print(Nd.translate(str.maketrans({"A": "T", "G": "C", "T": "A", "C": "G"})))
break
else:
print("NOPE! Input correct option!")
continue
break
#psyhological support in process of the coding - M.Kashtanov
It is a simple maze game I did for school. I have tried different solutions but still cant figure out how. I don't necessarily need a full solution; any tips would help.
x = 1
y = 1
valid_selection = True
while True:
if x == 1 and y == 1:
if valid_selection == True:
print("You can travel: (N)orth.")
valid_selection = True
choice = input("Direction: ")
if choice.lower() == "n":
y += 1
else:
print("Not a valid direction!")
valid_selection = False
elif x == 1 and y == 2:
if valid_selection == True:
print("You can travel: (N)orth or (E)ast or (S)outh.")
valid_selection = True
choice = input("Direction: ")
if choice.lower() == "n":
y += 1
elif choice.lower() == "e":
x += 1
elif choice.lower() == "s":
y -= 1
else:
print("Not a valid direction!")
valid_selection = False
elif x == 3 and y == 1:
print("Victory!")
break
You could have your function recieving x and y as parameters, removing the While True and replacing it with its creation, like def MyFunc(x,y), putting the valid_selection variable inside it and removing x and y attributions. And then you call it in the end of the code passing 1 and 1 as your desired parameters, like MyFunc(1,1)
There are many ways to create a function.
First, you need to abstract what your program do. The name of the function have to express this.
Second, you need to know what are the inputs. In your case the input must be x and y, or the proper names to these input.
The answer which your function will return is a important thing to know. The user will see a print or the user of the function will use a number, string or other object to feed other function.
I hope this not confusing.
I'm trying to finish writing this function that contains five different options and uses a While loop to allow the user to enter in their choice with the entry '5' exiting the loop. Below is the code I have so far, I'm having trouble completing the menu part within the def_main function. I keep getting an error after else:
break
Any input would be appreciated. Thank you for reading.
def main():
menuOption = 0
while 1 == 1:
print("1. Expanded Sum\n2. Reverse Expanded Sum\n3. Reverse Integer\n4. Product Table\n5. Exit\n")
menuOption = int(input("Enter correct menu option: "))
while menuOption<1 or menuOption>5:
print("Incorrect menu option!!")
menuOption = int(input("Enter correct menu option: "))
if menuOption == 5:
return
while 1 == 1:
num = int(input("Enter positive Integer: "))
if num <= 0:
print("You have entered negative integer or zero.")
continue
else:
break
if menuOption == 1:
printSum(num, int(False))
elif menuOption == 2:
printSum(num, int(True))
elif menuOption == 3:
print(str(reverseInt(num)))
elif menuOption == 4:
printProductTable(num)
if __name__ == "__main__": main()
def printSum(n, reverse):
s = sum(range(n+1))
if reverse:
print('+'.join(str(i) for i in range(1, n+1)) + ' = ' + str(s))
else:
print('+'.join(str(i) for i in range(n, 0, -1)) + ' = ' + str(s))
def reverse_int(n):
Reverse = 0
while(n > 0):
Reminder = n %10
Reverse = (Reverse *10) + Reminder
n = n //10
print(Reverse)
def printProductTable(n):
for row in range(1,n+1):
print(*("{:3}".format(row*col) for col in range(1, n+1)))
What is the error you are getting at the break?
It looks like your spacing might be off in the continue, I assume your else goes to the if at the top of the statement, but your continue does not match with it.
Rather than doing while 1==1 you can write while True. And also you have already checked while menuOption<1 or menuOption>5. So if your menuOption is a negative number it already falls into this condition as, say, -2 < 1.
And also seems like your code is not formatted. Means, continue is just above the else. It will generate the error. Re-formate your code. Give proper indentation.
this is a crabs simulator. Im having trouble with my while loop. Where it says
while val == True:
Is where the problem happens. It stays in the while loop but nothing happens. If you find anything, I will be most grateful.
Here is the full code. (I have tried to validate everything)
import time
import random
control1 = False
control2 = True
x = True
val = True
z = True
def throw(n):
for i in range(1,n+1):
dice_1 = random.randint(1,6);dice_2 = random.randint(1,6)
print "roll",i,", die 1 rolled",dice_1,"and die 2 rolled",dice_2,",total",dice_1+dice_2
time.sleep(2)
return n
while z == True:
if x == True:
while control1 == False:
try:
amount_1 = int(raw_input("Welcome to crabs.\nHow many times would you like to roll:"))
control1 = True
except ValueError:
print ("Enter a valid number.")
throw(amount_1)
x = False
else:
while val == True:
roll_again = raw_input("Would you like to roll again: ")
if roll_again == "1":
val = False
while control2 == True:
try:
amount_2 = int(raw_input("How many times would you like to roll:"))
control2 = False
except ValueError:
print ("Enter a valid number.")
throw(amount_2)
z = True
elif roll_again == "2":
val = False
exit()
else:
val = True
After your first run through the program x and val are both False, but z is still True. As a result, the outer loop just keeps on rolling.
Put this line:
print z, x, val
Underneath that while statement.
You'll see that after you respond to the "Would you like to roll again: " question with "2", both x and val are false. That means that it'll go through every part of your if..else statement and just keep looping back infinitely.
It's stuck in an endless loop after the else branch (of if x) is executed, because you set the value to False. In the next iteration you then say while val == True and since this statement is not False and there is no other statement to consider, you run in an endless loop.
To see what I mean simply add a print statment here:
else:
print val
while val == True:
roll_again = raw_input("Would you like to roll again: ")
if roll_again == "1":
Now, I don't know if you need all those booleans for your actual program, but if I'd to make it work, I'd start eliminating the booleans I don't need. I think you have a too complex structure.
Edit:
Here's a suggestion to make the program simpler.
import time
import random
x = True
z = True
def throw(n):
for i in range(1,n+1):
dice_1 = random.randint(1,6);dice_2 = random.randint(1,6)
print "roll",i,", die 1 rolled",dice_1,"and die 2 rolled",dice_2,",total",dice_1+dice_2
time.sleep(2)
return n
def ask(x):
if x:
print "Welcome to crabs."
try:
amount = int(raw_input("How many times would you like to roll:"))
except ValueError:
print ("Enter a valid number.")
throw(amount)
while z:
ask(x)
x = False
roll_again = raw_input("Would you like to roll again: ")
if roll_again == "1":
continue
else:
break