Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7913061
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T13:45:20+00:00 2026-06-03T13:45:20+00:00

I have found a function which implement a switch statement –> File = open(‘/file.txt’,’r’)

  • 0

I have found a function which implement a switch statement –>

File = open('/file.txt','r')

    String = File.readline()
    String = str(String)
    print String 

    for case in switch(String):
        if case("Head"):
            print "test successed"
            break
        if case("Small"):
            print String
            break
        if case("Big"):
            print String
            break  
        if case():
            print String 
            break 

the String value when I print it is Head, but the switch statement always goes to the last case.. the function works fine apparently because when I changed the String with v = “Head” it worked!!!

any idea what went wrong ?

the switch function –>

class switch(object):
 def __init__(self, value):
    self.value = value
    self.fall = False

 def __iter__(self):
    """Return the match method once, then stop"""
    yield self.match
    raise StopIteration

 def match(self, *args):
    """Indicate whether or not to enter a case suite"""
    if self.fall or not args:
        return True
    elif self.value in args: # changed for v1.5, see below
        self.fall = True
        return True
    else:
        return False
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T13:45:22+00:00Added an answer on June 3, 2026 at 1:45 pm

    Please don’t ever write code like this. Do it in proper Python style. It’s much easier to read. Anyone coming across usage of that “switch” mess will probably curse you heartily.

    with open('/file.txt', 'r') as fp:
        line = fp.readline().rstrip('\n')
        print line
    
    if line == 'Head':
        print "test successed"
    elif line == 'Small':
        print line
    elif line == 'Big':
        print line
    else:
        print line
    

    As to why it’s failing, the readline() call will very likely be including a trailing newline character, and 'Head' != 'Head\n'.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm learning JQuery and have found the factory function $() with its selectors quite
I have done various tests and I have found that the jQuery validate function
I found a very interesting/strange thing about MAX() function in SQL. I have column
I have found numerous examples on uploading a file to a web server via
I have found a Tutorial here on how to implement drag and drop in
Which php array function should be used to match a string to the first
For simplicity, let's say I want to do implement a function which takes two
Im trying to implement some code i found on a website which duplicates a
To implement a callback function from the native code to Java code, I have
I have a function A() in which I execute eight other 'sub-functions' which all

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.