LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-01-2023, 02:12 AM   #1
jackx2023
Member
 
Registered: Jan 2023
Posts: 32

Rep: Reputation: 12
My python3 script not doing what I want it to do


I am very new to python. I was testing my little knowledge of python by creating a simple for/if loop.

Here is my code

Code:
#!/usr/bin/python3                         
matched =[]
count = 0
win_nums = ('1','15','20','31','50')

mynums = input("Enter your numbers: ")

for i in mynums:
     if i in win_nums:
         count += 1
         matched.append(i)

print (f"you mantch {count} numbers. They are {matched}")
When I run it, I get this below

Code:
./lottocheck.py
Enter your numbers: 1 17 25 31 51
you mantch 4 numbers. They are ['1', '1', '1', '1']
It should have given me 1 and 31 with 2 matches.

I tried changing the line from if i in win_nums: to if i == win_nums: and I get

Code:
./lottocheck.py 
Enter your numbers: 1 17 25 31 51
you mantch 0 numbers. They are []
Any ideas what I am doing wrong. I appreciate any help. Thanks
 
Old 04-01-2023, 02:21 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,850

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
first I would try to add some print statements to see what's going on:
Code:
#!/usr/bin/python3                         
matched =[]
count = 0
win_nums = ('1','15','20','31','50')

mynums = input("Enter your numbers: ")

for i in mynums:
     print(i)                   # like this
     if i in win_nums:
         count += 1
         matched.append(i)

print (f"you mantch {count} numbers. They are {matched}")
 
Old 04-01-2023, 02:46 AM   #3
jackx2023
Member
 
Registered: Jan 2023
Posts: 32

Original Poster
Rep: Reputation: 12
@ pan64

The fix was simple. I needed to use the split() function for the mynums arrary. Like so:

for i in mynums.split():

Now when I run it

Code:
/lottocheck.py 
Enter your numbers: 20 31 50 77 99
you mantch 3 numbers. They are ['20', '31', '50']

./lottocheck.py 
Enter your numbers: 1 15 20 31 50
you mantch 5 numbers. They are ['1', '15', '20', '31', '50']


The hard thing about python is remembering the string function and modules. lol :)
 
Old 04-01-2023, 04:57 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,850

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
that is great. In that case you might want to mark the thread as solved.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
python3 -V returns Python 2.7.12, can't get python3 to work. gene_ Ubuntu 15 03-18-2019 10:39 AM
[SOLVED] making python3.6.4 default python3 Astral Axiom Linux - Newbie 17 04-14-2018 10:55 AM
[SOLVED] how to start python3.6 interpreter just by typing python in terminal not python3.6 bmohanraj91 Linux - Newbie 4 05-10-2017 07:51 AM
After upgrade python3.4 to python3.5.1 , not able to install packages "request" though pip3 YOGESHAS87 Linux - Software 1 08-03-2016 10:38 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:21 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration