Home > database >  itertools.cycle(['eng', 'rus']).__next__ not works properly
itertools.cycle(['eng', 'rus']).__next__ not works properly

Time:03-28

Ayo, guys Im started to learn python 2 days ago and started with simple Translator

My problem is: I wanted to write "@" to the console, my values change, but I don't know how to achieve that "Toggle" effect, when when writing "@" is chekcing if language number 1 is enabled and I change it to language number 2 and vice versa, if Language number 2 is enabled then switch to Language number 1

I found a solution on the Internet by:

var = itertools.cycle(['1', '2']).__next__

However, I can't get langtoggle to give me values one by one

At the moment, I'm stuck on this moment, which gives me value number 2 and does not want to change it to value 1

Please tell me what am I doing wrong?

Thank you :)

import itertools

langtoggle = itertools.cycle(['eng', 'rus']).__next__
engstroke = 'ENGLISH > RUSSIAN'
russtroke = 'RUSSIAN > ENGLISH'
lstroke = engstroke


while True:
    print (lstroke)
    word = input('Введите слово: ')
    if word == '@': 
        while True:
            langtoggle()
            if langtoggle() == 'eng':
                lstroke = engstroke
            if langtoggle() == 'rus':
                lstroke = russtroke
                break

to be honest, I need a solution what let me change to 1,2,3 and more values by same action so thats why I dont want to use boolean value for that problem

Also, I notice that "var()" cycling to next value even if im just checking this var in "if var() = 1" that looks strange, but I dont understand a huge things in coding right now, so..

CodePudding user response:

In your while block you're calling langtoggle() three times. Change this to be called only once.

Something like this:

import itertools

langtoggle = itertools.cycle(['eng', 'rus']).__next__
engstroke = 'ENGLISH > RUSSIAN'
russtroke = 'RUSSIAN > ENGLISH'
lstroke = eng


while True:
    print (lstroke)
    word = input('Введите слово: ')
    if word == '@': 
        while True:
            toggled = langtoggle()
            if toggled  == 'eng':
                lstroke = engstroke
            if toggled  == 'rus':
                lstroke = russtroke
                break

This is your code but modified by setting toggled to the result of the first call to langtoggle(). Then the tests are looking at the value of toggled instead of separate calls they were making previously.

  • Related