Python Forum
Beginner requiring help please
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Beginner requiring help please
#1
Hi

Am using lockdown to teach myslef Python (mainly using youtube, although have ordered a few books)

Please could somebody show/tell me how I would convert this into code or point me in the direction of where i could find the answer

Variable = 0
user inputs Y or N
if user input = Y then let variable = variable + 4

I appreciate many newbies will be asking for help at them minute but would greatly appreciate any help at all

Thanks

w4ldom4ths
Reply
#2
Variable = 0 is Python. If the variable is not already defined in the current scope (read about namespaces and scope) it creates the variable, the it assigns the variable the value zero.

For a console program you can use answer = Input('Would you like to play a game?')

for the comparison there are lots of ways to compare strings depending on how strict you want to be with the input. At the very least I would allow entering 'Y', 'Yes', 'y', 'yes', 'YES'. Maybe I would allow anything that starts with an upper or lower case 'y' to be Y and everything else is N
if answer[0] in('Y', 'y'):
    # do stuff
else:
    # do other stuff
Other ways to do this involve using regular expressions (regex) or you can convert the input to upper or lower case before making the comparison.

You should look at the official python documentation. It is online here:

https://docs.python.org/3/reference/index.html

You should also familiarize yourself with the standard library:

https://docs.python.org/3/library/index.html
Reply
#3
You should check the validity of condition, like in spoken language: if this condition is met add 4 to that value.

>>> spam = 0                                                                        
>>> bacon = 'Y'                                                                     
>>> if bacon == 'Y': 
...    spam += 4 
...                                                                                
>>> spam                                                                           
4
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#4
Thank You both for your help.

Much appreciated
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [SOLVED] Requiring help running an old Python script (non Python savvy user) Miletkir 13 5,452 Jan-16-2021, 10:20 PM
Last Post: Miletkir

Forum Jump:

User Panel Messages

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