[ 3 / biz / cgl / ck / diy / fa / ic / jp / lit / sci / vr / vt ] [ index / top / reports ] [ become a patron ] [ status ]
2023-11: Warosu is now out of extended maintenance.

/sci/ - Science & Math


View post   

File: 67 KB, 471x694, 1330199386872.jpg [View same] [iqdb] [saucenao] [google]
5090162 No.5090162 [Reply] [Original]

total = 0
month = raw_input('Print the current month in all underscore: ')
day = input('Print dya of the month with integers: ')
try:
number = int(day)
except ValueError:
print(day, "is not an integer.")
else:
if month == "january":
total + day
print month, day, 'is the day #', total
elif month == "february":
total = 31 + day
print month, day, 'is the day #', total
else:
print 'you misspelled the month'


why doesn't this check is the day input is integer or not. it just crashes. WTF

>> No.5090172
File: 26 KB, 600x400, __strawberry_1276159569.gif [View same] [iqdb] [saucenao] [google]
5090172

bump

>> No.5090177

By "just crashes," what do you mean? Does it compile? Do you get any output?

Also,
> if month == "january"
> total + day
I'm assuming that's wrong.

>> No.5090181

>>5090177
its just another if loop for each of the 1 2months.
you are right. it doesn't crash. it says
"day = input('Print day of the month with integers: ')
File "<string>", line 1
NameError: name 'h' is not defined"

this happens if i put "h" instead of an integer for variable day

>> No.5090184

>>5090177
what i am tryign to do is make it say that the input is not an integer, when i input somethign other then a number

>> No.5090187

Use raw_input instead of input.

input evaluates the entered text.

So when input encounters h, python thinks, "oh shit, where is variable h?"

I think.

Just use raw_input().

>> No.5090188
File: 216 KB, 677x903, 1338433889818.jpg [View same] [iqdb] [saucenao] [google]
5090188

day = input('Print day of the month with integers: ')
try:
number = int(day)
except ValueError:
print(number, "is not an integer.")
else:
print 'integer'


this is the part that doesn't want to work.
if i enter anything but a number it says"File "Untitled", line 1
day = input('Print day of the month with integers: ')
File "<string>", line 1
NameError: name 'h' is not defined"

how do i make it work?

>> No.5090190

>>5090177
does python code compile, fucking pleb

>> No.5090192

>>5090187
day = raw_input('Print day of the month with integers: ')
try:
number = int(day)
except ValueError:
print(number, "is not an integer.")
else:
print 'integer'


i try to say "h"

it returns:
ile "Untitled", line 5
print(number, "is not an integer.")
NameError: name 'number' is not defined

>> No.5090194
File: 100 KB, 381x384, 1332473251637.jpg [View same] [iqdb] [saucenao] [google]
5090194

bump

>> No.5090195

>>5090192
Well, the problem is probably here:

>number = int(day)

First, this calls int() on day. If that succeeds, it sets number to the result. Calling int() on day causes an error, which is caught by the try and passed to the except clause. So, number isn't actually ever set to anything.

>> No.5090199

>>5090195
this is my second week learning python. i'm trying to understand what you're telling me, but i'm doomed for tonight. how do i fix this?
i need it to return"not an integer" if the input is anything but a number

>> No.5090203

>>5090190
> doesn't realize that Python doesn't need to be run in an interpreter
> doesn't understand benefits of debugging through compilation

>> No.5090204

>>5090199
Something like:
print(day, "is not an integer.")

Because day has been set to the raw input.
The variable number doesn't get set to anything before the exception occurs.

>> No.5090209

input() inputs a string and tries to execute it. raw_input() just inputs the string. So just use raw_input() instead.

http://docs.python.org/library/functions.html#input

>Equivalent to eval(raw_input(prompt)).
>...
>Consider using the raw_input() function for general input from users.

>> No.5090210

>>5090204
omfg yes!
thank you.

P.S.

why won't something like this work?

x = raw_input('now')
if x == int:
print 'integer'
else:
print 'not an int'

>> No.5090214
File: 92 KB, 500x346, 12 Angry Men.jpg [View same] [iqdb] [saucenao] [google]
5090214

total = 0
day = input('Print day of the month with integers: ')
try:
number = int(day)
except ValueError:
print(day, "is not an integer.")
else:
total = 0
month = raw_input('Print the current month in all underscore: ')
if month == "january":
total + day
print month, day, 'is the day #', total
elif month == "february":
total = 31 + day
print month, day, 'is the day #', total
elif month == "march":
total = 59 + day
print month, day, 'is the day #', total
elif month == "april":
total = 90 + day
print month, day, 'is the day #', total
elif month == "may":
total = 120 + day
print month, day, 'is the day #', total
elif month == "june":
total = 151 + day
print month, day, 'is the day #', total
elif month == "july":
total = 181 + day
print month, day, 'is the day #', total
elif month == "august":
total = 212 + day
print month, day, 'is the day #', total
elif month == "september":
total = 243 + day
print month, day, 'is the day #', total
elif month == "october":
total = 273 + day
print month, day, 'is the day #', total
elif month == "november":
total = 304 + day
print month, day, 'is the day #', total
elif month == "december":
total = 334 + day
print month, day, 'is the day #', total
else:
print 'you misspelled the month'

>> No.5090216

>>5090214
it fucked up again. it worked by itself, but when i put it back into the program, it says that error and 'h' is not defined. why would i need to define the input?

>> No.5090217

>>5090210
Well, I'm not a python expert, but "a == b" usually means "is a equal to b?" not "is a one kind of b?".

Some languages have a typeof operator. Don't know about python. Your thought process isn't unreasonable, though!

>> No.5090221

>>5090216
I've already explained that you should use raw_input() rather than input().

If you raw_input 6+4 python understands that to be the string "6+4".

If you input 6+4 python attempts to evaluate that, and understands it as 10.

If you raw_input h+5 python understands it to be the string "h+5".

If you input h+5, python attempts to evaluate it, and looks for variable h...

>> No.5090222

>>5090217
It would be
type(x) == int
but raw_input does not return type int (integer); it returns type str (string).

>> No.5090226

>>5090222
Gotcha, thanks for the explanation.

OP, I'm off to bed. Good luck with your debugging.

Read the python tutorial, and refer to example code whenever you can. S'all over the internet.

>> No.5090225
File: 57 KB, 1000x277, 1000px-Hemingway_chart.jpg [View same] [iqdb] [saucenao] [google]
5090225

day = raw_input('Print day of the month with integers: ')
try:
number = int(day)
except ValueError:
print(day, "is not an integer.")
else:
month = raw_input('Print the current month in all underscore: ')
if month == 'january':
print month, day, 'is the day #', day
elif month == 'february':
print month, day, 'is the day #', int(day)+31


this finally works

>> No.5090230

last question.

after the error, the execution of the script stops.
how do i make it repeat? as in keep asking until a correct type of answer is given?

>> No.5090231

PROTIP: Don't reinvent the wheel.

import time

month = raw_input('Print the current month in all underscore: ')
day = input('Print dya of the month with integers: ')

try:

print month, day, 'is the day #', time.strptime(month+" "+day, "%B %d").tm_yday

except:

print "you dun goofed"

>> No.5090241

>>5090230

import time

while True:
~~~~month = raw_input('Print the current month in all underscore: ')
~~~~day = raw_input('Print dya of the month with integers: ')

~~~~try:
~~~~~~~~total = time.strptime(month+" "+day, "%B %d").tm_yday
~~~~~~~~break
~~~~except:
~~~~~~~~print "try again, dumbass"

print month, day, 'is the day #', total

>> No.5090242

>>5090231
i don;t know what those % mean, too complicated at this point. i'm a newfag at programming

>> No.5090243

>>5090242
That specifies the format that the parse function (strptime) should expect the entered date to be in.

http://www.tutorialspoint.com/python/time_strptime.htm

>> No.5090246

>>5090243
thanks.

i'm happy it works, no matter how bad the script is

>> No.5090248
File: 1.62 MB, 1831x941, 1346796588725.jpg [View same] [iqdb] [saucenao] [google]
5090248

Assume that fruit is a list containing strings representing the names of various fruit. Give a
command or a sequence of commands that produces a single string diet containing all the fruit
from the list, alphabetized and with spaces and with a ‘+’ sign between each fruit.

how would i do this?

>> No.5090252 [DELETED] 

>>5090248
"+".join(fruit)

>> No.5090251

>>5090248
what does it mean to give a command?
does it mean i need to enter something in order for it to be executed? and then won't it give the ame list back?

>> No.5090255

>>5090248
" + ".join(sorted(fruit))

>> No.5090259

>>5090255
could you please elaborate...

>> No.5090262

>>5090259

In order to produce a single string diet containing all the fruit from the list, alphabetized and with spaces and with a ‘+’ sign between each fruit, enter the command " + ".join(sorted(fruit))

>> No.5090263

>>5090262
where woudl i put that in the code? just print it in the shell?

>> No.5090266

>>5090262
yes. i am a retard. thank you

>> No.5090267
File: 47 KB, 500x356, 1348241709905.jpg [View same] [iqdb] [saucenao] [google]
5090267

fruit = ['apple', 'orange', 'pear', 'apricot']
diet =" + ".join(sorted(fruit))
print diet

programming isn't that bad

>> No.5090284

this is the last one, and i'm finished.

Assume that person is a string of the general form:
‘firstName middleName lastName’
Give a command or a series of commands that result in the creation of the corresponding string
‘firstName middleInitial. lastName’. For example: if person = ‘Henry Joseph
Carr’, the result should be ‘Henry J. Carr’.

any tips on how to split the input? or should i keep it intro 3 different inputs?

>> No.5090287
File: 189 KB, 1024x768, 1344626986775.jpg [View same] [iqdb] [saucenao] [google]
5090287

bump

>> No.5090294

>>5090284
inp = raw_input("asdf: ")
l = split(inp)
l[1] = l[1][0]+"."
print " ".join(l)

>> No.5090311

>>5090294
person = raw_input("Enter full name: ")
l = person.split()
l[1] = l[1][0]+"."
print " ".join(l)


this worked. thank you

>> No.5090331
File: 114 KB, 700x508, 431599_3305502761606_1389143338_4088619_147517030_n.jpg [View same] [iqdb] [saucenao] [google]
5090331

fuck fuck fuck

there is one more part.

i'll do most of it myself, but i can't figure out how to do one thing.

Write a program that asks the user for their height and weight and reports the BMI. The user
should enter the height in a format such as 5’ 8” to signify 5 feet 8 inches

how do i format..say from 3' 11" to 47inches?

>> No.5090332

>>5090331
if it is a string input that is...

>> No.5090336
File: 605 KB, 600x558, 1345690427958.png [View same] [iqdb] [saucenao] [google]
5090336

bump

>> No.5090344
File: 280 KB, 964x867, LahFt.jpg [View same] [iqdb] [saucenao] [google]
5090344

>>5090336
last bump

>> No.5090349

weight = input('Enter your weight in pounds: ')
height = input('Enter your height in inches : ')
BMI = 703.*(float(weight)/height**2)
print 'Your BMI is: ',BMI

this is jus tinches. what do i do, if the user enters 6' 2"?

>> No.5090352

>>5090349
First, you need to check if it contains " or '
then, you can use str.split() - look up the function
use str.split("'") and str.split('"'") (these are different, look close)
Finally, pick the right numbers and do some math work