Python Forum
These simple equations comes up as an error
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
These simple equations comes up as an error
#1
I dont know what is going wrong and I would like to fix this.

DB = input()

x = "float(DB)/10"
y = "float(x)+11"
z = "float(y)/100"



print ("Is this the date of your birthday?"+z)
Thanks, M3RCY
Reply
#2
Accepting that the logic is entirely flawed in the first place,
I think this is what you intended:

import datetime

try:
   DB = input("Date of Birth: ('yyyy-mm-dd'): ")
   datetime.datetime.strptime(DB, '%Y-%m-%d')
except ValueError:
   raise ValueError("Incorrect data format, should be YYYY-MM-DD")

x = float(DB[:4])
y = float(x+11)
z = float(y/100)

print ("Is {} the date of your birthday?".format(z))
Not sure what you are trying to do.
There are many ways to manipulate dates in existing python libraries
see: https://pypi.python.org/pypi?%3Aaction=s...mit=search
Reply
#3
If I put it into context, would it make it easier? I am trying to make the value of "x" in the y equation and the "y" in the z equation. It carries on coming up with an error. Here is the full python code to put it into context:

print ("Hello!")
print ("What is your name?")

myName = input()
print ("It is good to meet you, " + myName)

import os
os.system('start calc.exe')

wait = input("Press ENTER to continue.")

print ("What day were you born on? E.g. 23rd of February= 23.")
print ("Multiply your number by 7 on the calculator provided.")
wait = input("Press ENTER to continue.")

print ("Subtact 1 from your answer.")
wait = input("Press ENTER to continue.")

print ("Multiply this by 13.")
wait = input("Press ENTER to continue.")

print ("What month were you born in? E.g. January= 1, February= 2.")
wait = input("Press ENTER to continue.")

print ("Add this to your current total.")
wait = input("Press ENTER to continue.")

print("Add 3")
wait = input("Press ENTER to continue.")

print("Multiply by 11")
wait = input("Press ENTER to continue.")

print ("Subtract your day of birth from your current total.")
wait = input("Press ENTER to continue.")

print ("Subtract the month of your birth.")
wait = input("Press ENTER to continue.")

print ("Please tell me the answer. If it isn't correct, I cannot work out your date of your birth.")

DB = input()

x = "float(DB)/10"
y = "float(x)+11"
z = "float(y)/100"



print ("Is this the date of your birthday?"+z)

The purpose of the program is to work out the day and month you were born on in a maths equation.
Reply
#4
Since you did not post your code within code tags as you are supposed to, I will only comment on the code you supplied in the first post.

The way you have it written, 'DB' will be a string. 'x', 'y' and 'z' are also strings, since they are enclosed in quotes. You need to make 'DB' numeric and you need to remove the quotes from 'x', 'y' and 'z', so they actually are equations.
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#5
(Jun-26-2017, 09:00 PM)M3RCY Wrote: x = "float(DB)/10"
y = "float(x)+11"
z = "float(y)/100"
Those are strings. Strings are what they are, they aren't equations. If you want to do math, stop putting things in a string.

ie:
>>> DB = 4
>>> x = "float(DB)/10"
>>> y = "float(x)+11"
>>> z = "float(y)/100"
>>> x
'float(DB)/10'
>>> y
'float(x)+11'
>>> z
'float(y)/100'
>>>
>>> x = float(DB)/10
>>> x
0.4
>>> y = x+11
>>> y
11.4
>>> z = y/100
>>> z
0.114
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Simple code error please help bntayfur 7 3,389 Jun-08-2020, 02:22 AM
Last Post: pyzyx3qwerty
  Adding second message to simple loop error PythonGainz 2 2,054 Apr-06-2020, 11:55 AM
Last Post: PythonGainz
  Beginner - simple package installation error mefeng2008 0 1,697 Mar-13-2020, 09:17 AM
Last Post: mefeng2008
  Simple python code error cls0724 5 3,270 Mar-12-2020, 07:45 PM
Last Post: stullis
  Error with simple "or" statement? Mark17 4 2,264 Nov-15-2019, 05:16 PM
Last Post: Mark17
  Simple question - ERROR UnitedK 1 2,369 Sep-24-2018, 11:11 AM
Last Post: wavic
  Can't figure out the syntax error, relatively simple code maho686868 3 3,079 Jul-08-2018, 03:43 PM
Last Post: volcano63
  Simple syntax error help paulmj7 4 3,526 Aug-02-2017, 09:56 AM
Last Post: buran
  Syntax error for simple script pstein 2 3,978 Jun-26-2017, 03:46 PM
Last Post: snippsat
  Simple tictactoe with error mut4ntch1ck 2 3,097 Feb-22-2017, 11:28 PM
Last Post: mut4ntch1ck

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020