[Solved] Python: How to call index 100

you forgot that counting starts at 0 so its 0-99 and not 1-100. The 100th element is at string.printable[99] EDIT: assuming you want to loop around, you should use the modulus operator % so to access the 100th item in a 100 item list you would do: string.printable[99%100] to get the 180th item in a … Read more

[Solved] Checking 2 int variables

It appears that you want to check if the user’s number contains the same digit as the computer-generated numbers. If you only ever going to have two digits numbers you can get away with if str(RandomNum) == str(Guess)[::-1]:. This will check if the string value of RandomNum is equal to the string value of Guess … Read more

[Solved] How to connect Python consumer to AWS MSK [closed]

Using kafka-python: from kafka import KafkaConsumer if __name__ == ‘__main__’: topic_name=”example-topic” consumer = KafkaConsumer(topic_name, auto_offset_reset=”earliest”, bootstrap_servers=[‘kafka2:9092′], api_version=(0, 10), consumer_timeout_ms=1000) for msg in consumer: print(msg.value) if consumer is not None: consumer.close() from time import sleep from kafka import KafkaProducer # publish messages on topic def publish_message(producer_instance, topic_name, key, value): try: key_bytes = bytes(key, encoding=’utf-8’) value_bytes = … Read more

[Solved] Detect sign changes in an array [closed]

This is a simple code that resolve your problem/exemple: l = [ 1, 2, 6, -3, -2, -5, 6, 7, 1, -1, -3] for i in range(0, len(l)-1): p = l[i] * l[i+1] if p > 0: print(‘ok’) elif l[i+1] < 0: print(‘sell’) else: print(‘buy’) And if you want a new column in your dataframe, … Read more

[Solved] Incorrect pattern displayed

Assuming you want it to print out 6 patterns of 6 stars with a line between, this is what you want to do: import sys n = 0 a = 0 while (n < 6): n = n + 1 a=0 while(a < n): sys.stdout.write(‘*’,end=””) a = a +1 print ” solved Incorrect pattern displayed

[Solved] Write a Python program that repeatedly asks the user to input coin values until the total amount matches a target value [closed]

You can try something like this: MaxAmount = 100 TotalAmount = 0 while TotalAmount < MaxAmount: #Here if you want it to be more precise on decimals change int(raw_input(“Amount: “)) to float(raw_input(“Amount: “)) EnteredAmount = float(raw_input(“Amount: “)) if EnteredAmount > MaxAmount: print “You can not go over 100” elif TotalAmount > MaxAmount: #You can go … Read more

[Solved] why are the top five color red red red red red?

You are accidentally changing the entire dictionary instead of the specific alien you want the dictionary to represent. For now, just add .copy() to each time you append an alien dictionary to your random list of aliens. if color[random_num] == ‘green’: aliens_1.append(alien_0.copy()) In the future, this is probably best handled with classes rather than dictionaries. … Read more

[Solved] Python Code that returns true while key is pressed down false if release?

On some systems keyboard can repeate sending key event when it is pressed so with pynput you would need only this (for key ‘a’) from pynput.keyboard import Listener, KeyCode def get_pressed(event): #print(‘pressed:’, event) if event == KeyCode.from_char(‘a’): print(“hold pressed: a”) with Listener(on_press=get_pressed) as listener: listener.join() But sometimes repeating doesn’t work or it need long time … Read more

[Solved] How to catch specific index error in Python and attach new value for this? [closed]

There must be a more elegant answer to that but you can do : def extract_values(values): try: first = values[0] except IndexError: first = None try: second = values[1] except IndexError: second = None try: third = values[3] except IndexError: third = None return first, second, third 2 solved How to catch specific index error … Read more

[Solved] Row Average CSV Python

You can use the csv library to read the file. It is then just a case of calculating the averages: import csv with open(‘example.csv’) as handle: reader = csv.reader(handle) next(reader, None) for row in reader: user, *scores = row average = sum([int(score) for score in scores]) / len(scores) print ( “{user} has average of {average}”.format(user=user, … Read more