Im trying to create and run a program that allows the user to insert 2 drug names and receive their outcome. This is what my code looks like so far, yet I cant figure out the syntax error or if it's the best way to go about my plan
a = int(input("A.C"))
B = int(input("Opioid"))
sum = a + b
print("Incompatible, increased death rate", sum)
In your code, you make an assignment to B and then attempt to read from b. Those are two different variables. All names in Python are case-sensitive.
You need to change the variable name(b), also I added the new line when user wants to enter a value. I used fprint to print the output.
a = int(input("A.C \n") )
b = int(input("Opioid \n"))
sum = a+b
print(f'Incompatible, increased death rate, {sum}')
Related
I was working on building a randomized character generator for Pathfinder 3.5 and got stuck.
I am using the Populate_Skills(Skill_String, Draw, Skill_List, Class_Skill): function to populate a randiomized list of skills with their class based points total, class bonus, and point buy. So modelling the action of a player picking skills for their character.
As an example below, Wizards.
I pick Knowledge_Arcana as a skill and spend one of my skill point pool (Calculated by taking my intelligence modifier +2) on it. So that skill now equals my intelligence modifier(+1 in this case), class skill bonus as a wizard (+3), plus the point I spent(+1) for a total of 5.
The problem is while the function prints the correct result of 5, the outstanding variables do not populate with the final total. To continue our example I'd run the function on Knowledge_Arcana, get a +5, and then check the Knowledge_Arcana after the function call and get just +1. Conversely, if I write out the function as just an if statement it works. Example is next to the function for comparison.
Does anyone know why Im getting the different result?
## Creating the lists and breaking into two separate sections
Int_Mod = 1
Skill_Ranks = 3
Rand_Class = 'Wizard'
Knowledge_Arcana = Int_Mod
Knowledge_Dungeoneering = Int_Mod
Wizard_Class_Top_Skills = ["Knowledge_Arcana"]
Wizard_Class_Less_Skills = ["Knowledge_Dungeoneering"]
Class_Skill = 3
Important_Skills_Weighted = .6
Less_Important_Skills_Weighted = .4
Important_Skills_Total_Weighted = round(Skill_Ranks*Important_Skills_Weighted)
Less_Skill_Total_Weighted = round(Skill_Ranks*Less_Important_Skills_Weighted)
Wiz_Draw =['Knowledge_Arcana', 'Knowledge_Dungeoneering']
def Populate_Skills(Skill_String, Draw, Skill_List, Class_Skill):
if Skill_String in Draw:
Skill_List = Skill_List + Class_Skill + Draw.count(Skill_String)
print(Skill_String, Skill_List)
else:
print('Nuts!')
## Function Calls
Populate_Skills('Knowledge_Arcana', Wiz_Draw, Knowledge_Arcana, Class_Skill)
Populate_Skills('Knowledge_Dungeoneering', Wiz_Draw, Knowledge_Dungeoneering, Class_Skill)
print(Knowledge_Arcana,Knowledge_Dungeoneering)
Edited to be a MRE, I believe. Sorry folks, Im new.
You are passing in a reference to a list and expect the function to modify it; but you are reassigning the variable inside the function which creates a local variable, which is then lost when the function is exited. You want to manipulate the same variable which the caller passed in, instead.
def Populate_Skills(Skill_String, Draw, Skill_List, Class_Skill):
if Skill_String in Draw:
Skill_List.extend(Class_Skill + Draw.count(Skill_String))
print(Skill_String, Skill_List)
else:
print('Nuts!')
Alternatively, have the function return the new value, and mandate for the caller to pick it up and assign it to the variable.
def Populate_Skills(Skill_String, Draw, Skill_List, Class_Skill):
if Skill_String in Draw:
Skill_List = Skill_List + Class_Skill + Draw.count(Skill_String)
print(Skill_String, Skill_List)
else:
print('Nuts!')
return Skill_List
Skill_List = Populate_Skills('Knowledge_Arcana', Wiz_Draw, Knowledge_Arcana, Class_Skill)
# etc
You should probably also rename your variables (capital letters should be used for classes and globals; regular Python functions and variables should use snake_case) and avoid using global variables at all. The entire program looks like you should probably look into refactoring it into objects, but that's far beyond the scope of what you are asking.
I wrote a program that receives name from user checks if it is already taken in database and if it's not prints "OK". If name is taken program must make new name using old name + number. I keep getting "time limit exceed" error but i don't know what's wrong. I am new to programming so do not judge me strictly.
Here is my code:
n = int(input())
names = []
def CheckDB(name):
for i in names:
if i == name:
return(True)
return(False)
def MakeNewName(name, number):
while CheckDB(name+str(number)):
number+=1
newName = name+str(number)
names.append(newName)
return(newName)
def CreateNewUser(name):
if CheckDB(name):
return(MakeNewName(name, 1))
names.append(name)
return("OK")
for i in range (n):
name = input()
print(CreateNewUser(name))
Input looks like this:
100000
hgtyyvplfrlcr
dcvexvhgtyyvplfrlcryws
hmidcvexvhgtyyvplfrlcryw
vexvhgtyyv
idcvexvhgtyyv
vhgt
midcvexvhgtyyvplfrlcry
yv
lfrl
gtyyvplfrlcryw
xvhgtyyvplfrlcryws
yv
midcvexvhgtyyvplfrlcry
hmidcve
vexvhgtyyv
dcvexvhgtyy
midcvexvhgty
id
xvhgtyyvpl
midcvexvhgtyyvplfrlc
idcvexvhgtyyvplfr
idcvexvhgtyyvplfrl
dcvexvhgtyyv
midcv
midcvexvhgt
idcvexvhgtyyvplfrlcr
midcvexvhgtyy
yvplfrlcryw
midcvexv
l
dcvexvhgtyy
dcv
midcvexvhgtyyvplfrlc
vexvhgtyyvplfrlcry
yvpl
hmidcvexvhgtyyvplfr
And so on
p.s. sorry for my bad English
Python has no built-in 'time limit exceeded' error and your code doesn't show any time limit or what is being timed, so it's hard to say exactly what is going on, but note that time taken by naive linear search of a list grows linearly with the length of the list, and you're doing it many times for names already taken. If you instead use a set to store names, you can check if a name is taken using name in names and this will always take a constant amount of time no matter how large your 'database' grows.
Keep in mind this wouldn't matter if you were using an actual database, as the underlying database engine would handle efficiently indexing primary key columns for you.
I'm completely new to python, and I wanted to create a program that "loads" a number that the user would have entered.
To do this, I made a function with an input variable,
percentage
that I then tried to transform to an interfer,
percentage_int
To then put in a while loop.
However, I get an error message, why?
def loader():
percentage = input("what percentage do you want?")
percentage_int =int(percentage)
x = 0
print("Goal:{} %".format(percentage_int))
while x < percentage_int:
x+=1
print(x)
loader()
You need to do the type conversion, that is in this case from string to integer.
If you dont do so python will consider percentage_int as the input string itself.
percentage = input("what percentage do you want?")
percentage_int = int(percentage)
Go through this tutorial which will help you learn more about type conversions with python.
I would first like to apologize for how much of a beginner I am, however I have hit this wall after many other hurdles. The basis of this is to retrieve a value from a website, modify it using variables and print the final value. My knowledge of classes and objects is very very minimal. I just cannot figure out how I can take the value numTotal from my function getPlays and use it later to print as Final. The value prints correctly from within the function I just need to store that value for later use as a variable.
class GetPlaycount(object):
def getPlays(self):
print "Working"
browser = webdriver.PhantomJS('C:\Python27\phantomjs-2.0.0-windows\phantomjs.exe')
browser.get('https://osu.ppy.sh/u/4973241')
time.sleep(1)
Plays = browser.find_element_by_xpath('//*[#id="general"]/div[8]').text
numPlays = int(re.sub('[^0-9]', '', Plays))
numTime = int(numPlays) * int(numLength)
numTotal = int(numTime) * float(numVar)
print int(numTotal)
return numTotal
myClassObject = GetPlaycount()
myClassObject.getPlays()
Final = ????
print Final
raw_input("wait")
Thank you for your help and patience.
If I understand the question correctly
final = myClassObject.getPlays()
print final
Should be all you need.
I'm starting out in python.. The details I have written in the below.. It goes to an infinite loop and give me an error when I try to call the function inside itself.. Is this kind of recursion not allowed ?
Posting code below.. Thanks for all your help :)
The program assumes that we have 100 passengers boarding a plane. Assuming if the first one has lost his boarding pass, he finds a random seat and sits there. Then the other incoming passengers sit in their places if unoccupied or some other random seat if occupied.
The final aim is to find the probability with which the last passenger will not sit in his/her own seat. I haven't added the loop part yet which
would make it a proper simulation. The question above is actually a puzzle in probability. I am trying to verify the answer as I don't really follow the reasoning.
import random
from numpy import zeros
rand = zeros((100,3))
# The rows are : Passenger number , The seat he is occupying and if his designated seat is occupied. I am assuming that the passengers have seats which are same as the order in which they enter. so the 1st passenger enter has a designated seat number 1, 2nd to enter has no. 2 etc.
def cio(r): # Says if the seat is occupied ( 1 if occupied, 0 if not)
if rand[r][2]==1:
return 1
if rand[r][2]==0:
return 0
def assign(ini,mov): # The first is passenger no. and the second is the final seat he gets. So I keep on chaning the mov variable if the seat that he randomly picked was occupied too.
if cio(rand[mov][2])== 0 :
rand[mov][2] = 1
rand[mov][1] = ini
elif cio(rand[mov][2])== 1 :
mov2 = random.randint(0,99)
# print(mov2) Was used to debug.. didn't really help
assign(ini,mov2) # I get the error pointing to this line :(
# Defining the first passenger's stats.
rand[0][0] = 1
rand[0][1] = random.randint(1,100)
m = rand[0][1]
rand[m][2]= 1
for x in range(99):
rand[x+1][0] = x + 2
for x in range(99):
assign(x+1,x+1)
if rand[99][0]==rand[99][1] :
print(1);
else :
print(0);
Please tell me if y'all get the same error.. ALso tell me if I am breaking any rules coz thisi sthe first question I'm posting.. Sorry if it seems too long.
This is how it should've been...
The code does work fine in this case with the following mods :
def assign(ini,mov):
if cio(mov)== 0 : """Changed here"""
rand[mov][2] = 1
rand[mov][1] = ini
elif cio(mov)== 1 : """And here"""
mov2 = random.randint(0,99)
assign(ini,mov2)
I am using Python 2.6.6 on Windows 7, using a software from Enthought Academic Version of Python.
http://www.enthought.com/products/getepd.php
Also the answer to this puzzle is 0.5 which is actually what I am getting(almost) by running it 10000 times.
I didn't see it here but it had to be available online..
http://www.brightbubble.net/2010/07/10/100-passengers-and-plane-seats/
Recursion, while allowed, isn't your best first choice for this.
Python enforces an upper bound on recursive functions. It appears that your loop exceeds the upper bound.
You really want some kind of while loop in assign.
def assign(ini,mov):
"""The first is passenger no. and the second is the final seat he gets. So I keep on chaning the mov variable if the seat that he randomly picked was occupied too.
"""
while cio(rand[mov][2])== 1:
mov = random.randint(0,99)
assert cio(rand[mov][2])== 0
rand[mov][2] = 1
rand[mov][1] = ini
This may be more what you're trying to do.
Note the change to your comments. Triple-quoted string just after the def.
you may be able to find the exact solution using dynamic programming
http://en.wikipedia.org/wiki/Dynamic_programming
For this you will need to add memoization to your recursive function:
What is memoization and how can I use it in Python?
If you just want to estimate the probability using simulation with random numbers then I suggest you break out of your recursive function after a certain depth when the probability is getting really small because this will only change some of the smaller decimal places (most likely.. you may want to plot the change in result as you change the depth).
to measure the depth you could add an integer to your parameters:
f(depth):
if depth>10:
return something
else: f(depth+1)
the maximum recursion depth allowed by default is 1000 although you can change this you will just run out of memory before you get your answer