Thursday 15 July 2010

(Beginners Python) Creating if/else statements dependent on user input? -



(Beginners Python) Creating if/else statements dependent on user input? -

i'm trying create simple script will inquire question user input reply (or prompt selectable answers appear?), , programme output response based on input.

for example, if say

prompt1=input('can create stupid thing work?')

i have along lines of

if prompt1='yes': print('hooray, can!') else prompt1='no': print('well did anyway!') elif prompt1=#an reply wouldn't yes or no #repeat prompt1

i'm going wrong way. please descriptive possible learning exercise me. in advance!

you pretty close. read tutorial :)

#!python3 while true: prompt1=input('can create stupid thing work?').lower() if prompt1 == 'yes': print('hooray, can!') elif prompt1 == 'no': print('well did anyway!') else: print('huh?') #an reply wouldn't yes or no while true loop programme forever. use == test equality. use .lower() create easier test answers regardless of case. if/elif/elif/.../else right sequence testing.

here's python 2 version:

#!python2 while true: prompt1=raw_input('can create stupid thing work?').lower() if prompt1 == 'yes': print 'hooray, can!' elif prompt1 == 'no': print 'well did anyway!' else: print 'huh?' #an reply wouldn't yes or no raw_input used instead of input. input in python 2 tries interpret input python code. print statement instead of function. don't utilize () it.

python if-statement

No comments:

Post a Comment