I am trying to make a program that will calculate your popularity and based on that, use an algorithm to cycle through a friend list and open txt files based on that, then calculate their popularity, and if it is in a radius of 10 popularity points, they are put as compatible. However, my code is not working and my if statement keeps returning true no matter what. However, when printing the popularities, it says some are in my range, and some aren't. Why is my code failing?
def popularity(G, So, Sm, Sp):
g = float(G) * 0.3
so = float(So) * 0.4
sm = float(Sm) * 0.1
sp = float(Sp) * 0.2
return g + so + sm + sp
gfactor = input("What is your G-Factor? ")
social = input("What is your Social Quotient? ")
smartness = input("What is your smartness level? ")
sports = input("What is your sports skill? ")
poplr = popularity(gfactor, social, smartness, sports)
print(str(math.ceil(poplr * float(10))) + "%")
print("Finding friends... ")
for i in friends:
fs = open(i + ".txt")
json_str = fs.read(37)
data = json.loads(json_str)
friend_poplr = popularity(data["g"], data["So"], data["Sm"], data["Sp"]) * float(10)
if friend_poplr > float(poplr) - float(10):
if friend_poplr < float(poplr) + float(10):
print("# " + i + " is a compatible friend #")
else:
print("# " + i + " is not a compatible friend #")
else:
print("# " + i + " is not a compatible friend #")
Yes, I have imported math and json, and also have a list of friends, but I am censoring their names. PLEASE HELP!!!
I think the problem is the fact that you multiply friend_polr by 10 and you dont do the same for poplr. All you need to do is multiply poplr by 10 to make them both even. In my example below i made some other minor improvements to your code
def popularity(G, So, Sm, Sp):
g = float(G) * 0.3
so = float(So) * 0.4
sm = float(Sm) * 0.1
sp = float(Sp) * 0.2
return g + so + sm + sp
gfactor = input("What is your G-Factor? ")
social = input("What is your Social Quotient? ")
smartness = input("What is your smartness level? ")
sports = input("What is your sports skill? ")
poplr = popularity(gfactor, social, smartness, sports) * 10
print(str(math.ceil(poplr)) + "%")
print("Finding friends... ")
for i in friends:
fs = open(i + ".txt")
json_str = fs.read(37)
data = json.loads(json_str)
fs.close() # remember to close the file
friend_poplr = popularity(data["g"], data["So"], data["Sm"], data["Sp"]) * 10
if friend_poplr > poplr - 10:
if friend_poplr < poplr + 10:
print("# " + i + " is a compatible friend #")
continue
print("# " + i + " is not a compatible friend #")
Related
I need my program to generate as many playing fields as the entered number of players playing the game. Currently it only generates 1 field. Cant wrap my head around this one.
s = int(input("Sisesta mängijate arv: "))
b = int(input("Sisesta väjaku suurus: "))
b = b + 1
bb = b
c = int(input("Sisesta korrutustabeli suurus: "))
for s in range (1 , s+1):
numberolemas = ['0']
t.write(str(s) + ". väljak \n\n")
print(str(s) + ". väljak \n")
for bb in range (1 , bb):
for b in range (1 , b):
import random
number = random.randint(1, c) * random.randint(1, c)
olemas = True
while olemas:
number = random.randint(1, c) * random.randint(1, c)
if number not in numberolemas:
olemas = False
t.write(str(number))
print(str(number), end = ' ')
if number <= 9:
t.write(" ")
print(" ", end = '')
elif number >= 100:
t.write(" ")
print(" ", end = '')
else: t.write(" ") and print(" ", end = '')
numberolemas.append(number)
b = b + 1
t.write("\n\n")
print(" ")
bb = bb + 1
print(" ")
t.close() ```
I just started making a game like thing, and for some reason, the elif loop isn't doing anything when "upgrade" is entered.
choclate = 0
multiplier = 1
multipliercost = 10
x = 1
while x == 1:
if input() == (("choclate") + str(choclate+multiplier)):
choclate = choclate+multiplier
print("\nYou now have " + str(choclate) + " choclate.\nMultiplier Upgrade Cost: " + str(multipliercost) + " choclate\n")
elif input() == "upgrade":
multiplier = multiplier*2
choclate = choclate-multipliercost
multipliercost = multipliercost*2.5
print("You have upgraded your multiplier to " + str(multiplier))
I am very new to coding, so I don't really know what to call this problem.
If you call input() twice, then the user needs to key in twice in each round.
if you expect the user to key in only once, then you also need to call input() once in each round, and store it into a variable.
Here is the fix.
choclate = 0
multiplier = 1
multipliercost = 10
x = 1
while x == 1:
# save the input into variable
key_in = input()
print(("choclate") + str(choclate+multiplier))
if key_in == (("choclate") + str(choclate+multiplier)):
choclate = choclate+multiplier
print("\nYou now have " + str(choclate) + " choclate.\nMultiplier Upgrade Cost: " + str(multipliercost) + " choclate\n")
elif key_in == "upgrade":
multiplier = multiplier*2
choclate = choclate-multipliercost
multipliercost = multipliercost*2.5
print("You have upgraded your multiplier to " + str(multiplier))
I'm making a small program that shoots out math problems and requires an answer to pass. It works fine, but all the randint values I generate stay static for as long as the progran is running. I figured if I change:
Tehtävä = random.choice(Laskut)
Into a function it should refresh with the loop. Problem is I can't for the life of me figure out how to do that. Would it even work for what I'm trying? The randint values are determined in a seperate list. Heres the rest of the code:
Peli = 1
while Peli != 2:
pulma = 1
refresh = 1
Tehtävä = random.choice(Laskut)
while pulma == 1:
ratkaisu = float(input(Tehtävä.problem + "\n:"))
if ratkaisu == Tehtävä.answer:
pulma += 1
refresh += 1
print("oikein")
elif ratkaisu == "loppu":
pulma += 1
refresh += 1
Peli += 1
else:
print("väärin")
Here are the values I used:
import random
class Algebra:
def __init__(self, problem, answer):
self.problem = problem
self.answer = answer
#Muuttujat
#erotus ja summa
a = random.randint(1,99)
b = random.randint(1,99)
c = random.randint(1,99)
d = random.randint(1,99)
#jako ja kerto
e = random.randint(1,10)
f = e*random.randint(1,10)
g = random.randint(1,10)
#Kysymykset
Kysymys_M = [str(a) + "+" + str(b) + "-x=" + str(c),
str(a) + "-" + str(b) + "-x=" + str(a),
str(a) + "-" + str(b) + "-" + str(c) + "-x=" + str(d),
str(e) + "*x=" + str(f),
str(f) + ":x=" + str(e),
"x:" + str(e) + "=" + str(g)]
#Vastaukset
Vastaus_M = [a+b-c,
-b,
a-b-c-d,
f/e,
f/e,
e*g]
Laskut = [
Algebra(Kysymys_M[0], Vastaus_M[0]),
Algebra(Kysymys_M[1], Vastaus_M[1]),
Algebra(Kysymys_M[2], Vastaus_M[2]),
Algebra(Kysymys_M[3], Vastaus_M[3]),
Algebra(Kysymys_M[4], Vastaus_M[4]),
Algebra(Kysymys_M[5], Vastaus_M[5]),]
(If I have packed too much information please let me know.)
Python program to calculate the cost of sending a parcel.
The below program is giving a name error when trying to print the total cost:
price_of_parcel = float(input(" enter the price of parcel in rands (R) : R "))
total_distance = float(input(" enter the total distance to the parcel destination in (km): km "))
parcel_route = input(" do you want it by Air or Freight? (Air / Freight): ").strip().lower()
parcel_insurance = input(" do u want full insurance or limited insurance? ( Full / Limited) : ").strip().lower()
parcel_nature = input(" do you want parcel as gift or as no gift? (Gift / No Gift) : ").strip().lower()
parcel_urgency = input( " do you want a standard or priority delivery? ( Priority / Standadard ) : ").strip().lower()
if parcel_route == "Air" :
p_route = total_distance * 0.36
totalcost = p_route
if parcel_route == " freight " :
p_route = total_distance * 0.25
totalcost = p_route
if parcel_insurance == " full " :
p_insurance = 50.00
totalcost = p_route + p_insurance
if parcel_insurance == " limited " :
p_insurance = 25.00
totalcost = p_route + p_insurance
if parcel_nature == " gift" :
p_gift = 15.00
totalcost = p_route + p_insurance + p_gift
if parcel_nature == " no gift " :
p_gift = 0
totalcost = p_route + p_insurance + p_gift
if parcel_urgency == " Priority" :
urgency = 50.00
totalcost = p_route + p_insurance + p_gift + urgency
if parcel_urgency == " standard " :
urgency = 20.00
totalcost = p_route + p_insurance + p_gift + urgency
if parcel_route == "air" and (parcel_insurance == " full" or "limited") and (parcel_nature == "gift" or "no gift") and (parcel_urgency == "priority" or "standard"):
totalcost = totalcost = p_route + p_insurance + p_gift + urgency
print( " the total cost for the parcel is : " + str(totalcost))
else:
if parcel_route == "freight" and (parcel_insurance == " full" or "limited") and (parcel_nature == "gift" or "no gift") and (parcel_urgency == "priority" or "standard"):
totalcost = p_route + p_insurance + p_gift + urgency
print( " the total cost for the parcel is : " + str( totalcost))
The name error states that the variable created within the if statement is not defined.
I have tried a few things but the code does not seem to work.
I would predefine all the variables at the top i.e.
p_route = 0
p_insurance = 0
p_gift = 0
urgency = 0
You probably don't hit some of the if's with your input, creating one, or more of these variables.
Ah, and also - put the above more into "if - elif - else", you don't need to check some of the conditions, once some earlier ones are satisfied...
For your ref: https://www.tutorialspoint.com/python/python_if_else.htm
You bug is here -
parcel_route = input(" do you want it by Air or Freight? (Air / Freight): ").strip().lower()
parcel_route is a string in lower case and with no trailing or leading spaces. Because you have used strip() and lower()
But, in your if clause you have capital letter and spaces too!
if parcel_route == "Air" :
p_route = total_distance * 0.36
totalcost = p_route
if parcel_route == " freight " :
p_route = total_distance * 0.25
totalcost = p_route
As a result, neither of the two if block is executed and your p_route variable is never set!
I am working on a Hangman game, but I am having trouble replacing the dashes with the guessed letter. The new string just adds on new dashes instead of replacing the dashes with the guessed letter.
I would really appreciate it if anyone could help.
import random
import math
import os
game = 0
points = 4
original = ["++12345","+*2222","*+33333","**444"]
plusortimes = ["+","*"]
numbers = ["1","2","3"]
#FUNCTIONS
def firstPart():
print "Welcome to the Numeric-Hangman game!"
def example():
result = ""
ori = random.choice(original)
for i in range(2,len(ori)):
if i % 2 == 0:
result = result + ori[i] + ori[0]
else:
result = result + ori[i] + ori[1]
return ori
# def actualGame(length):
#TOP LEVEL
firstPart()
play = raw_input("Do you want to play ? Y - yes, N - no: ")
while (play == "Y" and (points >= 2)):
game = game + 1
points = points
print "Playing game #: ",game
print "Your points so far are: ",points
limit = input("Maximum wrong guesses you want to have allowed? ")
length = input("Maximum length you want for the formulas (including symbols) (must be >= 5)? ")
result = "" #TRACE
ori = random.choice(original)
for i in range(2,len(ori)):
if i % 2 == 0:
result = result + ori[i] + ori[0]
else:
result = result + ori[i] + ori[1]
test = eval(result[:-1])
v = random.choice(plusortimes) #start of randomly generated formula
va = random.choice(plusortimes)
formula = ""
while (len(formula) <= (length - 3)):
formula = formula + random.choice(numbers)
formula2 = str(v + va + formula)
kind = ""
for i in range(2,len(formula2)):
if i % 2 == 0:
kind = kind + formula2[i] + formula2[0]
else:
kind = kind + formula2[i] + formula2[1]
formula3 = eval(kind[:-1])
partial_fmla = "------"
print " (JUST TO TRACE, the program invented the formula: )" ,ori
print " (JUST TO TRACE, the program evaluated the formula: )",test
print "The formula you will have to guess has",length,"symbols: ",partial_fmla
print "You can use digits 1 to 3 and symbols + *"
guess = raw_input("Please enter an operation symbol or digit: ")
a = 0
new = ""
while a<limit:
for i in range(len(formula2)):
if (formula2[i] == partial_fmla[i]):
new = new + partial_fmla[i]
elif (formula2[i] == guess):
new[i] = guess
else:
new[i] =new + "-"
a = a+1
print new
guess = raw_input("Please enter an operation symbol or digit: ")
play = raw_input("Do you want to play ? Y - yes, N - no: ")
The following block seems problematic:
elif (formula2[i] == guess):
new[i] = guess
else:
new[i] =new + "-"
Python does not allow modification of characters within strings, as they are immutable (cannot be changed). Try appending the desired character to your new string instead. For example:
elif formula2[i] == guess:
new += guess
else:
new += '-'
Finally, you should put the definition of new inside the loop directly under, as you want to regenerate it after each guess.