Shoe size converter function - python

I am trying to make a shoe size converter function. But this program prints weird things like:
"You need function shoe_size at 0x030236F0> sized shoes"
What do I have to do? Here is my code:
def shoe_size(foot_size):
shoe_size = (foot_size + 1,5) * 3 / 2
return shoe_size
foot_size = (input("Enter your foot size: "))
print ("You need " + str(shoe_size) + " sized shoes")

There are a few errors or at least potential errors here check the changes I made to the input statement and you usually don't want a variable that is the same name as the function it is in so:
def shoe_size(given_size):
#foot_size = (foot_size + 1,5) * 3 / 2 #This multiples a tuple (, as .)
return (given_size + 1.5) * 3 / 2 #returning float (1.5 makes float)
foot_size = int(input("Enter your foot size: "))
#figured you wanted a type cast here: used int just change to float if halfs wanted
print ("You need " + str(shoe_size(foot_size)) + " sized shoes")
#this converts and prints the size: Your original was treating the function as a variable

You need to give the foot_size variable to your shoe_size method in your print statement:str(show_size(foot_size))

def shoe_size(foot_size):
shoe_size = (foot_size + 1.5) * 3 / 2
return shoe_size
foot_size = (input("Enter your foot size: "))
print ("You need " + shoe_size(foot_size) + " sized shoes")

This is the corrected script:
def shoe_size(foot_size):
shoe_size = (foot_size + 1.5) * 3 / 2
return shoe_size
foot_size = (input("Enter your foot size: "))
print ("You need " + str(shoe_size(foot_size)) + " sized shoes")

Related

How can I fix a bug using modulo (%) in Python?

I am making a converting program in Python, and in this case it’s from feet to yards. I have having trouble with a specific line of code that is not showing an error message, yet refuses to actually work.
When I run the program and put the following numbers in the input (3, 2, 4, 1) the Yards should be 8, and the feet should be 0, but it stays on 7 yards, and doesn’t add the 3 extra feet into the yard amount.
firstYard = int(input("Enter the Yards: "))
firstFeet = int(input("Enter the Feet: "))
secondYard = int(input("Enter the Yards: "))
secondFeet = int(input("Enter the Feet: "))
print("Yards: ")
print(int(firstYard + secondYard))
print(" Feet: ")
print(int(firstFeet + secondFeet) % 3)
if ((firstFeet + secondFeet) % 3) > 2:
** firstYard += 1
**
The last line is what I’m having trouble with.
Yards = int(input("Enter the Yards: "))
Feet = int(input("Enter the Feet: "))
Yards_2 = int(input("Enter the Yards: "))
Feet_2 = int(input("Enter the Feet: "))
print("\nYards:",(Yards * 3 + Yards_2 * 3 + Feet + Feet_2) // 3, "Feet:",(Yards * 3 + Yards_2 * 3 + Feet + Feet_2) % 3)
You have to scroll to the right more.
The feet should not exceed 2 because 3 feet would be a yard.
This line is causing your problem:
if ((firstFeet + secondFeet) % 3) > 2:
The remainder of division by three cannot be greater than 2, maybe you mean to see if three fits more than twice? Try division instead of modulo if that's the case.
Your test
if ((firstFeet + secondFeet) % 3) > 2:
will always be false, because you're taking the modulo of that sum and any number module 3 cannot be greater than 2.
What you need to add to your yard total is the integer division of the feet total:
firstYard += (firstFeet + secondFeet) // 3
I would structure your code differently:
totalYard = firstYard + secondYard + (firstFeet + secondFeet) // 3
totalFeet = (firstFeel + secondFeet) % 3

adding to a string based on the number of characters

the problem says to print "that's a really really ... big number" with one "really" for every extra digit that the number has (so 15 would be "that's a really big number", 150 would be "that's a really really big number", 1500 would be "that's a really really really big number", and so on.)
the input is an integer, and the only requirements listed are that the code should run correctly with any integer, should use a while loop to keep dividing the number by 10 and should use += to add onto the end of a string
x = input(("input an integer: "))
count = len(x)
y = int(x / 10)
countx = count - 1
print("that's a " + count("really") + " big number")
i don't really know what i did, but i can tell it's not correct
Try this one. Uses while loop to divide number by 10 and += to add onto the string. You will need to take a string variable and then append to it as count increases. Loop will run until number >= 10 as conditions you mentioned.
x = int(input(("input an integer: ")))
str=""
while x>=10:
x=x//10
str+="really "
print("that's a " + str + "big number")
Added the while loop as you stated in your question
strVar = ""
y = 0
length = 0
cnt = 0
finished = False
num = input("Type a number: ")
while not finished:
y = int(num)//10
length = int(len(str(y)))
if length <= cnt:
finished = True
else:
strVar += " really"
cnt += 1
print("That's a" + strVar + " big number!")
Give this a try. It finds how many trailing zeroes there are in a given number and based on that creates a certain amount of really's.
x = input(("input an integer: "))
count = int(len(x) - len(x.rstrip('0')))
if count == 0:count = 1
really = "really "*count
print(f"that's a {really} big number")
Try this
x = int(input("enter an integer"))
print("that's a " + 'really '*(len(str(x))-1) + " big number")
If you want to correct your function use this
x = input(("input an integer: "))
count = len(x)
y = int(x) / 10
countx = count - 1
print("that's a " + countx*" really" + " big number")

How to treat a var as both int and string.

def die():
first = str(randint(1, 6))
second = str(randint(1, 6))
total = first + second
print "You have rolled a " + first + " and a " + second + ", for a total score of " + total + "."
Standard die throwing game, but I'm struggling to print the values for individual die as well as the total. Treating as a string for individual, but then sum leads to concatenation rather than actual sum.
Thanks
Keep your variables as numbers and let print do the formatting:
def die():
first = randint(1, 6)
second = randint(1, 6)
total = first + second
print "You have rolled a", first, "and a", second, ", for a total score of", total, "."
Or you could do some formatting using str.format to have more control over the default inter-parameter spacing in the above:
print "You have rolled a {} and a {}, for a \
total score of {}.".format(first, second, total)
There are two ways to address your problem (and more still!). Firstly, you need to make sure you keep your integers as type int when adding them together, and then cast them to a string when you print them out.
You can do this like the below, using the str() casting method and + concatenation.
def die1():
"""Roll and print two dice using concat."""
first = randint(1, 6) # keep these as integers
second = randint(1, 6)
total = first + second # so addition works
# but now cast to str when printing
print "You have rolled a " + str(first) + " and a " + str(second) + ", for a total score of " + str(total) + "."
But a handier way is to use the str.format() method to put placeholders in your string, and then let python cast and format the integer values for you. If you have big numbers with 4 or more digits, an advantage of this would be that you can use a string formatting code like "my big number: {0:d,}".format(1000000) to make your string output like "my big number: 1,000,000", which is much more readable.
def die2():
"""Roll and print two dice using str.format()."""
first = randint(1, 6)
second = randint(1, 6)
total = first + second
# or use the str.format() method, which does this for you
print "You have rolled a {0} and a {1}, for a total score of {3}.".format(first, second, total)
You can use casting to change the structure of the var. You can either use them as strings and for total use this line:
total = int(first) + int(second)
or use them as int and cast them to string in the print by using str(first) and str(second)
Best
print "You have rolled a " + str(first)
This would convert the int to a string, hence concatenating it.
Also, you can do
total = int(first) + int(second) to address the first issue.
You have two solutions:
Convert the numbers back to int before adding them:
def die():
first = str(randint(1, 6))
second = str(randint(1, 6))
total = str(int(first) + int(second))
print ("You have rolled a " + first + " and a " + second + ", for a total score of " + total + ".")
Convert the numbers into str before printing them:
def die():
first = randint(1, 6)
second = randint(1, 6)
total = first + second
print ("You have rolled a " + str(first) + " and a " + str(second) + ", for a total score of " + str(total) + ".")
Either solution will work perfectly fine.
This would also work. Don't convert first and second to str until after you perform sum on them. Then remember to cast them as str in your print statement.
def die():
first = randint(1, 6)
second = randint(1, 6)
total = str(first + second)
print ("You have rolled a " + str(first) + " and a " + str(second) + ", for a total score of " + total + ".")

Creating a triangle of characters from a users input

For an assignment I'm suppose to make a triangle using the users input if the characters are equal to an even number. The triangle is suppose to print up to 5 lines in height and the left of it should be the left half of the string and the right side of the triangle should be the right side of the string.
Example of what the triangle is suppose to look like
The problem is I can't figure out how to divide my triangle in half without hard coding it or how to properly display the white space without a loop (were not allowed to in the assignment). Right now if I were to put in "ab" it would return:
aabb
aabbaabb
aabbaabbaabb
aabbaabbaabbaabb
aabbaabbaabbaabbaabb
Instead of:
aabb
aaaabbbb
aaaaaabbbbbb
aaaaaaaabbbbbbbb
aaaaaaaaaabbbbbbbbbb
Here's my code:
#GET Users String
userString = input("Please enter a string with a value of 7 or less characters: ")
#CALCULATE IF userString is less than or equal to 7 and is even
if len(userString) <= 7 and len(userString) % 2 == 0:
print (" " * 5 + userString)
print(" " * 4 + userString * 2)
print(" " * 3 + userString * 3)
print(" " * 2 + userString * 4)
print(" " + userString * 5)
#CALCULATE IF userString is less than 7 but and off
elif len(userString) <=7 and len(userString) % 2 == 1:
print("You are odd")
#CALCULATE IF userString is over 7 characters
else:
print ('The string is too long. \nGood-bye!')
Here's how you can do this:
def print_next(st, index):
if index < 6: # have not reached 5 - print offset and string
offset = 6-index
print ' '*offset+st
index=index+1 # increase counter
print_next((st[0:2]+st[-2:len(st)])*index,index) # recursively go next
print_next('aabb',1) # initial call with index set to 1
I think you can use a stack to save each line so you can easily get a triangle-like output. Also because you can't use loop so my suggestion would be recursive.
public_stack = []
def my_func(original_str, line_count, space_num):
if(line_count == 0):
return
times = line_count * 2
half_length = len(original_str) / 2
left_str = original_str[:half_length] * times
right_str = original_str[half_length:] * times
space_str = ' ' * space_num
complete_str = space_str + left_str + right_str
global public_stack
public_stack.append(complete_str)
space_num += len(original_str)
line_count -= 1
return my_func(original_str,line_count,space_num)
if __name__ == '__main__':
original_str = 'ab'
line_count = 5
space_num = 0
my_func(original_str,line_count,space_num)
global public_stack
for i in range(len(public_stack)):
line = public_stack.pop()
print line

Invalid Syntax Python?

Why is this code wrong (from Python 3.3.2). All it outputs is 'invalid syntax' when I've looked over the code lots of times:
#Get the numbers from the useer
a = int(input("Enter number a: "))
b = int(input("Enter number b: "))
c = int(input("Enter number c: "))
d = a*b*c #Make d a times b times c
#Display the results
print (str(a) + " mutiplied by " + str(b) + "multiplied by" + str(c) " equals " + str(d)))
This is what it should output:
Enter number a: 5
Enter number b: 10
Enter number c: 3
5 multiplied by 10 multiplied by 3 equals 150
Thanks in advance
You forgot a + operator in that last line, and you have an extra close paren at the end of that line.
print (str(a) + " mutiplied by " + str(b) + "multiplied by" + str(c) " equals " + str(d)))
^^^ ^^^

Categories