2021年2月4日星期四

Program becomes unresponsive but is still running

# Function that calculate sales totals and return output    def sales_total(quantity)    if quantity >= 100      price = 8      quantity * price    elsif quantity < 100 || quantity >= 50      price = 9      quantity * price    else      price = 10      quantity * price    end  end    # Function that displays output to screen    def display(quantity)    print "\nFor #{quantity} widgets, the total is: $#{sales_total(quantity)}"  end    # Check user input  def check_value(quantity)    bool = true    while bool      begin        ans = Integer(quantity)      rescue ArgumentError        puts "Error! Invalid Number!"        print "Enter a whole number: "        quantity = gets.chomp.to_i      end    end  end    # Start of program  puts "Welcome to the Widget Store!\n"    print "\nWould you like to purchase a widget? (y/n) "  user_choice = gets.chomp.downcase    while user_choice == 'y'      print "\nHow many widgets would you like? "    quantity = gets.chomp      # Valdiate if user input is a number    check_value(quantity)      # Call to sales_total    sales_total(quantity)      # Call to output    display(quantity)      # Check if user wants to continue    print "\n\nWould you like to purchase more widgets? (y/n) "    user_choice = gets.chomp.downcase    end    print "\nThanks for using our store! Come back soon!"      

Console put from program

Welcome to the Widget Store!

Would you like to purchase a widget? (y/n) y

How many widgets would you like? we Error! Invalid Number! Enter a whole number: 45

Cursor just continues to blink but will not take keyboard input

The program had no issues running before adding the check_value function. Not sure where I am going wrong and have tried several different solutions.

https://stackoverflow.com/questions/66057019/program-becomes-unresponsive-but-is-still-running February 05, 2021 at 11:07AM

没有评论:

发表评论